| | |
Strings and For Loops
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2005
Posts: 3
Reputation:
Solved Threads: 0
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++)
Code tags added. -Narue
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;
} 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)
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXLINE 256 int main(void) { char str[MAXLINE], rev[MAXLINE]; int i, length; for ( ; ; ) { /*-------------------------------------------- * Read string and determine length *--------------------------------------------*/ printf("Enter string or EOF to quit: "); if ( scanf ("%s", str) != 1 ) break; length = strlen(str); /*--------------------------------------------- * Build the reversed string and print it. * Don't forget to add the terminating 0! *---------------------------------------------*/ 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; }
I'm here to prove you wrong.
![]() |
Similar Threads
- how to read an array of strings in C++ (C++)
- Finding the union of two strings (C++)
- StringTokenizer, while loops and return statements (Java)
- Loops (C++)
- Strings (C++)
- Compare strings... (C++)
- C++ Address Book (C++)
- help for program involving switch loops and file (C++)
- comparing two strings with linear search.. (Java)
- JSP and Oracle (JSP)
Other Threads in the C++ Forum
- Previous Thread: Simple Program will not exit gracefully
- Next Thread: Why compiler complants error when compile.
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






