Thread: reverse an array

  1. #1
    Registered User
    Join Date
    Jan 2012
    Location
    New Zealand, Hastings
    Posts
    7

    reverse an array

    Hi
    So I want to reverse text. If I use the EOF marker, it compiles but does not output.' /n' suffices, but my input could be greater than one line.
    Code:
    #include <stdio.h>
    #define MAX 500
    
    int main()
    {
       char c, phrase[MAX];
       int i;
       printf("enter your phrase?\n");
       for(i=0;(c = getchar()) !='\n'; ++i)
          phrase[i] = c;
       for (--i; i >=0; --i)
       putchar(phrase[i]);
       putchar('\n');
    
       return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since getchar() returns an int, and EOF is an int, c should be an int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2012
    Location
    New Zealand, Hastings
    Posts
    7
    cheers laser

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reverse an array
    By wonderwall in forum C Programming
    Replies: 4
    Last Post: 10-22-2011, 01:03 PM
  2. Help with Reverse Array
    By Crcullen3916 in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2007, 08:47 PM
  3. How do you reverse an array?
    By ducksickii in forum C Programming
    Replies: 25
    Last Post: 04-01-2007, 05:39 AM
  4. reverse an array - help appreciated
    By Vireyda in forum C++ Programming
    Replies: 2
    Last Post: 03-21-2004, 12:52 PM
  5. reverse the order of an array
    By dustyd in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2003, 11:14 AM