| | |
C++ Array Question
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 8
Reputation:
Solved Threads: 0
I have a question about manipulating an array in C++. In my program, a cstring is entered. The string is printed. Finally, the string must be printed in reverse and displayed.
The final part is where I am having my problem. I have the strlen function determine the legnth of the function, and I was then going manipulate the string by having the array go to the last character of the string entered, and then print each of the characters by cycling through the c-string backwards.
I am having trouble with this. I can't seem to figure out how to get the strlen function to be the starting point of the array. Any suggestions?
Thank you.
The final part is where I am having my problem. I have the strlen function determine the legnth of the function, and I was then going manipulate the string by having the array go to the last character of the string entered, and then print each of the characters by cycling through the c-string backwards.
I am having trouble with this. I can't seem to figure out how to get the strlen function to be the starting point of the array. Any suggestions?
Thank you.
please post code so we don't have to guess what you are doing. But basically its like this
C++ Syntax (Toggle Plain Text)
char string[] = "Hello World"; int len = strlen(string)-1; while(len >= 0) printf("%c",string[len--]);
•
•
Join Date: Oct 2006
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
please post code so we don't have to guess what you are doing. But basically its like this
C++ Syntax (Toggle Plain Text)
char string[] = "Hello World"; int len = strlen(string)-1; while(len >= 0) printf("%c",string[len--]);
C++ Syntax (Toggle Plain Text)
strcpy(reversedUserInput, userInput, strlen(userInput)); for (reversedUserInput; i >= 0; i--) { cout << "The string reversed:\n" << reversedUserInput << endl; }
Thanks.
strcpy() function does not take 3 parameters --
>>for (reversedUserInput; i >= 0; i--)
This is incorrect. you have to initialize variable i with the length of reversedUserInput string minus 1 (because of null terminator)
>>cout << "The string reversed:\n" << reversedUserInput << endl
This is incorrect too. It will display the entire string in normal order (not reverse order). It should print just one character on each loop iteration.
C++ Syntax (Toggle Plain Text)
strcpy(reversedUserInput, userInput);
>>for (reversedUserInput; i >= 0; i--)
This is incorrect. you have to initialize variable i with the length of reversedUserInput string minus 1 (because of null terminator)
C++ Syntax (Toggle Plain Text)
for (i = strlen(reversedUserInput)-1; i >= 0; i--)
>>cout << "The string reversed:\n" << reversedUserInput << endl
This is incorrect too. It will display the entire string in normal order (not reverse order). It should print just one character on each loop iteration.
•
•
Join Date: Oct 2006
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
strcpy() function does not take 3 parameters --
C++ Syntax (Toggle Plain Text)
strcpy(reversedUserInput, userInput);
>>for (reversedUserInput; i >= 0; i--)
This is incorrect. you have to initialize variable i with the length of reversedUserInput string minus 1 (because of null terminator)
C++ Syntax (Toggle Plain Text)
for (i = strlen(reversedUserInput)-1; i >= 0; i--)
>>cout << "The string reversed:\n" << reversedUserInput << endl
This is incorrect too. It will display the entire string in normal order (not reverse order). It should print just one character on each loop iteration.
If you don't mind, can you see if the attached code looks better?
I am still confused about the the last part of code that you corrected where the cout is. I'm not exactly sure how to go about getting this to go one character at a time. Would something like reversedUserInput i++ be used? I see what you mean though. I get the string printed the amount of times that there are characters in the string.
C++ Syntax (Toggle Plain Text)
strncpy(reversedUserInput, userInput, strlen(userInput)); for (i = strlen(reversedUserInput)-1; i >= 0; i--) { cout << "The string reversed:\n" << reversedUserInput << endl; }
Thank you. Sorry for the questions, but I'm not quite understanding arrays totally yet.
Last edited by Juggler; Oct 27th, 2006 at 11:55 pm.
here is how I would code it.
cout << "The string reversed: ";
for (i = strlen(reversedUserInput)-1; i >= 0; i--)
{
// print the ith character
cout << reversedUserInput[i];
}
cout << "\n";•
•
Join Date: Oct 2006
Posts: 8
Reputation:
Solved Threads: 0
Thank you for your help. I didn't use your code exactly, but it helped me figure out what I was doing wrong with mine. Amazing how obvious stuff sometimes causes so much trouble. I spent a good 45 minutes trying to figure out why cout was printing each letter in it's own line. INSIDE and OUTSIDE of a loop seems to make a BIG difference!!!
Thanks again!
Thanks again!
![]() |
Similar Threads
- Little Simple array question :) (C)
- Simple array question (C++)
- Simple char array question (C)
- Newbie Constructor / Array question (C++)
- Array Question (C++)
- array question (C++)
Other Threads in the C++ Forum
- Previous Thread: Error: Cannot convert double ()() to double
- Next Thread: "conflicting types" error
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






