Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ prog. to reverse each word in sentence

  1. Nov 7, 2015 #1
    Code (C):
    #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.
     
  2. jcsd
  3. Nov 7, 2015 #2

    Mark44

    Staff: Mentor

    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).
     
  4. Nov 7, 2015 #3
    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?
     
  5. Nov 7, 2015 #4

    Mark44

    Staff: Mentor

    In C++, string is the name of a class. See http://www.cplusplus.com/reference/string/string/ for more information.
    If you declare an array to hold 10 characters, you shouldn't attempt to put more characters in than that.
    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.
    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; .
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C++ prog. to reverse each word in sentence
Loading...