8 Replies - 3479 Views - Last Post: 14 November 2011 - 12:53 PM Rate Topic: -----

#1 TheBigBangBoom   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 09-October 11

Program to print a message in reverse order

Posted 13 November 2011 - 07:44 PM

I am obviously doing something wrong. I modeled this off a program to print numbers in reverse order and nothing happens once I type in my character message. Any help on how to fix it? here is my initial code

#include <stdio.h>

#define N 100

int main (void)
{
    char a[N], *p;
    
    printf ("Enter a message: " );
    
    do
    {
    for (p = a; p < a + N; p++)
    scanf ("%c", p);
}while ("%c" != "\n");

    printf ("In reverse order: ");
    for (p = a + N - 1; p >= a; p--)
    printf ("%c", *p);
    
  
    
    fflush(stdin);
    getchar ();
}


edit by ishkabible: please use code tags when posting code. like so, :code: thank you :)

This post has been edited by ishkabible: 13 November 2011 - 08:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Program to print a message in reverse order

#2 buffalobill   User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: Program to print a message in reverse order

Posted 13 November 2011 - 08:09 PM

as you are using C (which i`m not very familiar with):
you need to use the function cin.getline(line,n) where line is a char type array named line and n is the number of its elements.
when you say reverse the message do you mean reverse the order of letters in each word or reverse(last first)the words or reverse both the word spelling and the word order?
Was This Post Helpful? 0
  • +
  • -

#3 TheBigBangBoom   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 09-October 11

Re: Program to print a message in reverse order

Posted 13 November 2011 - 08:12 PM

I mean just print it in reverse order, as far as using the cin.getline the only problem with that suggestion is we haven't reached that information yet in our class. All we can really use loops and write our own functions to do everything
Was This Post Helpful? 0
  • +
  • -

#4 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1857
  • View blog
  • Posts: 6,684
  • Joined: 19-February 09

Re: Program to print a message in reverse order

Posted 13 November 2011 - 08:42 PM

Hi, cin and getline are C++ so not applicable here.

Line 15 is not correct, you want to check the character just entered :

15	}while ("%c" != "\n");


Was This Post Helpful? 0
  • +
  • -

#5 TheBigBangBoom   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 09-October 11

Re: Program to print a message in reverse order

Posted 13 November 2011 - 08:50 PM

I tried changing that to while (p != '\n') and still does not do anything it will compile but I enter my message and nothing happens after that
Was This Post Helpful? 0
  • +
  • -

#6 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1857
  • View blog
  • Posts: 6,684
  • Joined: 19-February 09

Re: Program to print a message in reverse order

Posted 13 November 2011 - 10:54 PM

Almost there p is a pointer and you are comparing to a char. You could use the dereference operator *p or a subscript p[0] to return a value.
Was This Post Helpful? 0
  • +
  • -

#7 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: Program to print a message in reverse order

Posted 13 November 2011 - 11:24 PM

You can fix up your input loop ... and print loop starting address ...
/* enter line */
for( p = a; p < a+N; ++p )
{
   scanf( "%c", p );
   if( *p == '\n' ) break;
}
/* print line reversed */
for( --p; p >= a; --p )
{
   putchar( *p );
}


This post has been edited by David W: 13 November 2011 - 11:27 PM

Was This Post Helpful? 1
  • +
  • -

#8 TheBigBangBoom   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 09-October 11

Re: Program to print a message in reverse order

Posted 14 November 2011 - 06:47 AM

That worked perfectly. thanks David.
Was This Post Helpful? 0
  • +
  • -

#9 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: Program to print a message in reverse order

Posted 14 November 2011 - 12:53 PM

View PostTheBigBangBoom, on 14 November 2011 - 08:47 AM, said:

That worked perfectly. thanks David.


You are welcome ...

Actually the code below may be a little more efficent code ...using getchar()
... and I like to flush any buffer overflow, so stdin is empty before any next request for input from stdin ... (if this is what the program expects, and is often the case in beginning student problems, the oversight of which is often the cause of many needless student sleepless nights.)
/* enter line */
for( p = a; p < a+N; ++p )
{
   *p = getchar();
   if( *p == '\n' ) break;
}

if( *p != '\n' ) while( getchar() != '\n' ) ; /* 'flush' stdin ... */

/* print line reversed */
for( --p; p >= a; --p )
{
   putchar( *p );
}


This post has been edited by David W: 14 November 2011 - 01:34 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1