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
Click For Summary

Discussion Overview

The discussion revolves around a C++ programming task that involves reversing each word in a sentence. Participants are exploring the logic and coding techniques necessary to achieve this, including issues related to array sizes, input methods, and mixing C and C++ syntax.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • Some participants express uncertainty about how to reverse each word in a sentence and seek guidance on the proper code.
  • Concerns are raised about the size of the character arrays, with one participant noting that an array of size ten may be insufficient for many sentences.
  • There is a discussion about the mixing of C and C++ syntax in the provided code, particularly regarding input/output methods.
  • One participant questions whether 'string' is a keyword in C++ or only in Java, leading to clarification that it is a class in C++.
  • Participants mention using alternative input methods like 'cin.getline' and debate the appropriateness of using 'getch' and other C functions alongside C++ streams.
  • There is a query about the use of data types like 'int' and the 'return' keyword, with explanations provided about their roles in variable declarations and function returns.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to reverse words in a sentence, and multiple views on coding practices and syntax remain present throughout the discussion.

Contextual Notes

Limitations include the potential for buffer overflow with fixed-size arrays, the mixing of C and C++ functions, and the lack of clarity on the expected input size and handling.

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.
 
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];
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; .
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
5K
Replies
16
Views
6K
Replies
89
Views
7K
  • · Replies 30 ·
2
Replies
30
Views
5K
Replies
14
Views
4K
  • · Replies 15 ·
Replies
15
Views
8K