C++ prog. to reverse each word in sentence

  • Context: C/C++ 
  • Thread starter Thread starter Raghav Gupta
  • Start date Start date
  • Tags Tags
    C++ Reverse
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 3K views
Raghav Gupta
Messages
1,010
Reaction score
76
Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main(){
clrscr();
char a[10],b[40];
count<<"Enter sentence\n";
gets(a);
count<<a ;\\ How to reverse here?
getch();}
I don't know the proper code how to do the program of reversing each word in a sentence.
How does one gets logic of a problem? I know I have to reverse each word but for that what code.
I have written some code above.
Code is for C++ not C.
 
Physics news on Phys.org
Raghav Gupta said:
Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main(){
clrscr();
char a[10],b[40];
count<<"Enter sentence\n";
gets(a);
count<<a ;\\ How to reverse here?
getch();}
I don't know the proper code how to do the program of reversing each word in a sentence.
How does one gets logic of a problem? I know I have to reverse each word but for that what code.
I have written some code above.
Code is for C++ not C.
The words in a sentence are separated by a space, and possibly by multiple spaces. Your code will need to parse the input string to find the words, and will then need to reverse each word.

What do your two arrays have different sizes? Your a array can hold only ten characters, which would be too short for many sentences.

Also, your code seems to be a mix of C and C++, with some input/ouput done using C++ streams, and some input done using C standard library functions (gets and getch).
 
Mark44 said:
What do your two arrays have different sizes? Your a array can hold only ten characters, which would be too short for many sentences.

Also, your code seems to be a mix of C and C++, with some input/ouput done using C++ streams, and some input done using C standard library functions (gets and getch).
Is string a keyword in C++ or java only?
I have randomly given size to arrays but I was checking the program in turboC++ by borland 1992 DOS version and I inputted more then 10 characters.
Then also I was getting the output of all the letters. How that is so when the size is specified to 10?

We were simply told in class to use that header files and codes. An alternative I know is cin.getline.
Isn't getch appropriate? Should we use some other thing?
There are things like int and return but when to use what?
 
Raghav Gupta said:
Is string a keyword in C++ or java only?
In C++, string is the name of a class. See http://www.cplusplus.com/reference/string/string/ for more information.
Raghav Gupta said:
I have randomly given size to arrays but I was checking the program in turboC++ by borland 1992 DOS version and I inputted more then 10 characters.
Then also I was getting the output of all the letters. How that is so when the size is specified to 10?
If you declare an array to hold 10 characters, you shouldn't attempt to put more characters in than that.
Raghav Gupta said:
We were simply told in class to use that header files and codes. An alternative I know is cin.getline.
Isn't getch appropriate? Should we use some other thing?
You could use cin to get one character from the input stream. I think you would be better off not mixing C functions (getch() in conio.h and gets() in stdio.h) with the C++ functionality that uses the cin and count streams. It would be less confusing, IMO.
Raghav Gupta said:
There are things like int and return but when to use what?
int is a data type that is used in declaring variables that are to hold integers. Other data types are char, long, float, and double, as well as others. If you're expected to write a program in C++, your textbook should have had a section on variable declarations.
return is a keyword that typically is used at the end of a function (including main()). It does two things: 1) returns control to whatever called the function, 2) (if used with an expression) returns that value to the caller; for example, return value; .