Thread: Program to reverse a string

  1. #1
    Registered User
    Join Date
    Nov 2007

    Program to reverse a string

    Hey all,

    I have to write a program that takes in a string from the user and reverses the string (i.e. it takes in "Hello all nice to meet you" and prints out "you meet to nice all Hello"). We have to use fgets to read in the line of input from the user and then use strtok with " \n" as delimeters to separate each individual word in the string. I've thought about it for a long time but I just can't come up with the right algorithm.

    If the string is "birds and bees", how do I reverse this using the above method? My instructor used strtok(), sprintf(), strcpy(), and a temp array to iteratively create the reversed string. Any ideas are welcome because I am short on those, especially since this is supposed to be one of the easier ones in my assignment. Thanks for the help!

  2. #2
    Registered User
    Join Date
    Oct 2001
    You probably need a while() loop, using a call to strtok() as a condition. You also need an initial call to strtok() before the while, passing the string as an argument.

    The sprintf() is likely the function your instructor used to reverse the words, by using an appropriate format, so that the last word read is prepended to the result string, instead of appended.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Yeah thanks, I've got the while loop now that terminates when strtok returns NULL, so it can read each word now. However I still don't understand how to use the sprintf() to add the last word read to the existing string (there needs to be spaces in between the words too). I tried to use strcpy() and strcat() to generate the reversed string, but I have no idea how to put spaces to separate the words if I use them.

  4. #4
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Sydney, Australia
    char c;
    Use the above to check if something is a space.

    Firstly gather the string length.

    int len(char str[200]){
      int len;
    for( len = 0; str[len] != '\0'; len++ );
    return len;
    So once you have the string length.

    char space[200];
    int j = 0;
    int i = len; //index starting from end of string
    for(i = len; i >= 0; i--){    //Start going across the string from the end of the string
    if(isspace(c)){   //When a space is encountered
    space[j] = i;  //Store it into an array
    //You now have the index locations where the spaces occurred.
    //Now use another loop starting from the beginning of the spring, and putchar(' ');
    //when you come to that index location

    Not entirely sure about this though.

    Note you will have to edit this for your own use in your assignment. Because you will need some kind of while loop

    while((c=getchar()) != EOF){

    or something for most of this to work
    Last edited by JFonseka; 11-20-2007 at 04:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
Website Security Test