C/C++ C++ prog. to reverse each word in sentence

  • Thread starter Thread starter Raghav Gupta
  • Start date Start date
  • Tags Tags
    C++ Reverse
AI Thread Summary
The discussion revolves around a C++ program intended to reverse each word in a given sentence. The initial code snippet has several issues, including the use of the `gets` function, which is unsafe, and an array size of 10 characters, which is insufficient for most sentences. Participants emphasize the need to parse the input string to identify words and reverse them, suggesting that the code should avoid mixing C and C++ functions for clarity. There is also confusion regarding the `string` keyword in C++, which is clarified as a class, and discussions about proper data types and the use of `return` in functions. The conversation highlights the importance of understanding variable declarations and the correct usage of input/output functions in C++.
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];
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
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).
 
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 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; .
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top