reverse an array

This is a discussion on reverse an array within the C Programming forums, part of the General Programming Boards category; Hi So I want to reverse text. If I use the EOF marker, it compiles but does not output.' /n' ...

  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
    21,445
    Since getchar() returns an int, and EOF is an int, c should be an int.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 11:52 AM
  5. reverse the order of an array
    By dustyd in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2003, 10:14 AM

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