C++ prog. to reverse each word in sentence

  • C/C++
  • Thread starter Raghav Gupta
  • Start date
  • Tags
    C++ Reverse
In summary, the conversation discusses the implementation of a program to reverse each word in a sentence. The code provided is a mix of C and C++, with some input/output done using C++ streams and some input done using C standard library functions. The size of the arrays is discussed, as well as the use of different keywords such as string, int, and return. The appropriate use of these keywords is also mentioned.
  • #1
Raghav Gupta
1,011
76
Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main(){
clrscr();
char a[10],b[40];
cout<<"Enter sentence\n";
gets(a);
cout<<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.
 
Technology news on Phys.org
  • #2
Raghav Gupta said:
Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main(){
clrscr();
char a[10],b[40];
cout<<"Enter sentence\n";
gets(a);
cout<<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).
 
  • #3
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?
 
  • #4
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 cout 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; .
 

What is the purpose of a C++ program to reverse each word in a sentence?

A C++ program to reverse each word in a sentence is used to manipulate a given string by reversing the order of letters in each word. This can be useful for tasks such as encryption or word manipulation.

What is the input for a C++ program to reverse each word in a sentence?

The input for a C++ program to reverse each word in a sentence is a string containing a sentence or phrase. This can be entered by the user or provided through a file or other source.

What is the output of a C++ program to reverse each word in a sentence?

The output of a C++ program to reverse each word in a sentence is a string with the words in the original sentence reversed. For example, the input "Hello World" would result in the output "olleH dlroW".

What are the steps involved in a C++ program to reverse each word in a sentence?

The steps involved in a C++ program to reverse each word in a sentence include splitting the string into individual words, reversing the order of letters in each word, and then joining the words back together to form the reversed sentence. Additional steps may be needed for tasks such as removing punctuation or handling special characters.

Are there any limitations to a C++ program to reverse each word in a sentence?

Yes, there may be limitations to a C++ program to reverse each word in a sentence depending on the specific implementation. For example, the program may not be able to handle certain characters or may produce unexpected results if the input is not formatted correctly. It is important to thoroughly test the program and account for potential limitations.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
884
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
4
Views
736
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
8K
Back
Top