reverse string

This is a discussion on reverse string within the C Programming forums, part of the General Programming Boards category; How do I take an string and reverse its order. (ie abc to cba) using getchar() and putchar(). I can ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    3

    reverse string

    How do I take an string and reverse its order. (ie abc to cba) using getchar() and putchar(). I can read it using the following code.

    c = getchar();

    while(c != EOF)
    {
    codeWriter(c);/*This converts the string to a code*/
    c = getchar();
    }

    The output is in this function.
    char codeWriter(char c)
    {
    switch(c)
    {
    case 'A':
    {
    c = 'b';
    putchar(c);
    break;
    }
    .
    .
    .
    How do I reverse the order of the string

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    char store[256]={0};
    int count=0;
    char c;
    do
    {
    c=getchar();
    store[count]=c;
    ++count;
    } while(c!=EOF);
    that fills an array with user input. Now all you have to do is to putchar() the array out backwards. count will keep a track of how many chars were entered so with careful calculation you will know the index into the array of the last char entered then just count back printing in a for loop.
    Last edited by Stoned_Coder; 11-25-2001 at 07:46 AM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    I think they should put this one in the FAQ. I, also, have seen this countless times.

    --Garfield
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21