944,204 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3432
  • C++ RSS
Apr 9th, 2005
0

Strings and For Loops

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Starz20 is offline Offline
3 posts
since Apr 2005
Apr 9th, 2005
0

Re: Strings and For Loops

Throw the whole thing in a loop and break from the loop if the user enters something that isn't valid:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Simple Program will not exit gracefully
Next Thread in C++ Forum Timeline: Why compiler complants error when compile.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC