Strings and For Loops

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 3
Reputation: Starz20 is an unknown quantity at this point 
Solved Threads: 0
Starz20 Starz20 is offline Offline
Newbie Poster

Strings and For Loops

 
0
  #1
Apr 9th, 2005
Hello everyone. My instructor has asked me to write a program that accepts a string as input from a user and prints the string in reverse. I have to use a "for" loop to reverse the string. I am also suppose to allow the user to input more than one string. I think I have done a pretty job at all of the above except for allowing the user to input an additional string. I am taking an intro to C++ and am very new to C++. In the program I have done so far I have defined a character limit of 256. I am confused on how to alow the user to input an additional string. I am not really for sure where to justify this in the body of the program or if it has to be done in the beginning. Any help would be greatly appreciated.

I was thinking it would be here, but I changed the value it did not work.
for (i=0; i<length; i++)
#include  <stdio.h> 
#include  <stdlib.h> 
#include  <string.h>
#define MAXLINE 256
int main(void) 
	
{ 
  
     char str[MAXLINE], rev[MAXLINE];
     int i, length;
    
     /*--------------------------------------------
     *  Read string and determine length  
     *--------------------------------------------*/
     printf("Enter string:    ");
     scanf ("%s", str);
     length= strlen(str);
     /*---------------------------------------------
     *  Build the reversed string and print it.
     * Don't forget to add the terminating 0!
     *---------------------------------------------*/
      	/* loop 2 times */ this did not help
      for (i=0; i<length; i++) 
   rev[length-i-1] = str[i];
     rev[length] = 0;
     printf("reverse string:  %s\n\n", rev);
     /*---------------------------
     *  Palindrome?
     *---------------------------*/
     if (strcmp (str,rev) == 0) 
   printf ("Palindrome!\n\n");
     else
   printf ("Not a Palindrome\n\n");
     return 0; 
}
Code tags added. -Narue
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,582
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Strings and For Loops

 
0
  #2
Apr 9th, 2005
Throw the whole thing in a loop and break from the loop if the user enters something that isn't valid:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAXLINE 256
  5. int main(void)
  6. {
  7. char str[MAXLINE], rev[MAXLINE];
  8. int i, length;
  9.  
  10. for ( ; ; ) {
  11. /*--------------------------------------------
  12.   * Read string and determine length
  13.   *--------------------------------------------*/
  14. printf("Enter string or EOF to quit: ");
  15. if ( scanf ("%s", str) != 1 )
  16. break;
  17. length = strlen(str);
  18. /*---------------------------------------------
  19.   * Build the reversed string and print it.
  20.   * Don't forget to add the terminating 0!
  21.   *---------------------------------------------*/
  22. for (i=0; i<length; i++)
  23. rev[length-i-1] = str[i];
  24. rev[length] = 0;
  25. printf("reverse string: %s\n\n", rev);
  26. /*---------------------------
  27.   * Palindrome?
  28.   *---------------------------*/
  29. if (strcmp (str,rev) == 0)
  30. printf ("Palindrome!\n\n");
  31. else
  32. printf ("Not a Palindrome\n\n");
  33. }
  34.  
  35. return 0;
  36. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC