Thread: Interesting problem....reverse order of characters in a String!!!!

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    23

    Interesting problem....reverse order of characters in a String!!!!

    Hey guys Im slowly starting to learn C and i was quite interested in this problem...
    If i entered a line of characters say a sentence how would i be able to print out the reverse of the input. Eg
    Please enter string:
    I hate debugging.

    .gniggubed etah I (This is what the result should look like)

    Any ideas.....

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There are many ways to do it. Read one character at a time recursively. Read the entire line into a large buffer and walk backwards from the end. Give it a shot!


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Store it into an array and print the array backwards.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by slowprogrammer View Post
    Eg
    Please enter string:
    I hate debugging.

    .gniggubed etah I (This is what the result should look like)

    Any ideas.....
    When I first saw this, I wanted to say it was wrong, but it's not. I was looking for

    .gniggudeb etah I

    But then the rest of the characters would have to be backwards too! (I guess the "i" already is) ho ho ho.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A stack is a nice structure for reversing stuff.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by slowprogrammer View Post
    Hey guys Im slowly starting to learn C and i was quite interested in this problem...

    Any ideas.....
    So interested that you want someone else to tell you how to solve the problem instead of attempting it on your own, then when stuck ask a specific question. I hope your school assignment isn't due too soon.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    68
    a little c++ here, but you can figure it out...
    Code:
    int main(){
    
    char text[255];
    int i=0;
    cout>>"press z to stop"<<endl;
    while(i<255 || ){// dont uverrun the buffer 
        char k;
         cin>>k;
         if(k =='z') break;
    }
    for( ; i >0; i--){
        cout<<text[i];
    }
    return 0;
    }

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't hand out solutions or the newbies will not learn anything! I urge you to read the Homework Policy.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by smasherprog View Post
    a little c++ here, but you can figure it out...
    Code:
    int main(){
    
    char text[255];
    int i=0;
    cout>>"press z to stop"<<endl;
    while(i<255 || ){// dont uverrun the buffer 
        char k;
         cin>>k;
         if(k =='z') break;
    }
    for( ; i >0; i--){
        cout<<text[i];
    }
    return 0;
    }
    1) Stop providing complete solutions

    2) Stop writing C++ code on the C forum

    3) Stop living with the impression that C and C++ are similar so it doesn't really matter which one you use

    4) Stop assuming that the input will not contain the letter 'z'
    Last edited by claudiu; 05-04-2010 at 12:04 PM.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    5) What is that while loop's OR supposed to be doing?


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    How about using string.length() and a for loop to reverse it? C++ is cool, it's quite amazing what you can get to work with just playing with it!

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If we wanted C++, we'd be on the C++ forum.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by fadlan12 View Post
    How about using string.length() and a for loop to reverse it? C++ is cool, it's quite amazing what you can get to work with just playing with it!
    The same can be said about C and probably most languages.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  14. #14
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    Whoops, sorry about that. Was not trying to down any other languages.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's not your fault, you didn't bring up C++ on the C forum.

    That said, we'd like to hear from the OP again. Show us what you've got slo-pro!


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM