| | |
String manipulation as array of char
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 17
Reputation:
Solved Threads: 0
Ok so I posted this before but didn't get a response...
Our prof gave us this problem:
"Write a program that will get, from the user, a string of characters such as a sentence, and place this into a declared char array. Ask the user which letter they would like to replace, then ask the user to enter a letter that they would like to replace it with."
So I did something like this:
It kind of works, but I know that there are some mistakes which I am fixing...
What I want to know is this: How do I replace a letter in the array with another letter that is already stored in the array? This is not what we were asked but i'm wondering how you would go about implementing such a thing.
Example: User enters Hello world, replace the 7th word in the array with the first word entered.
Our prof gave us this problem:
"Write a program that will get, from the user, a string of characters such as a sentence, and place this into a declared char array. Ask the user which letter they would like to replace, then ask the user to enter a letter that they would like to replace it with."
So I did something like this:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> void main(void) { const int charString = 20; char sentenceHolder1[charString]; char sentenceHolder2[charString]; int index1; int index2; int index3; char replacement; char exitOnInput; int count = 0; cout << "Enter a sentence of up to 20 characters" << endl; cin.getline(sentenceHolder1, charString); cout << "Enter a character that you would like to be replaced" << endl; cin.getline(sentenceHolder2, charString); cout << "Enter what you would like it to be replaced with" << endl; cin >> replacement; for(index1=0; index1 < charString; index1++) { for(index2=0; index2 < charString; index2++) { while ( sentenceHolder1[index1] == sentenceHolder2[index2] ) { count = count+1; sentenceHolder1[index1] = replacement; } }} for ( index3 = 0; index3 < charString; index3++) { cout << sentenceHolder1[index3]; } cout << "Enter a letter followed by 'enter' to exit" << endl; cin >> exitOnInput; return 0; }
It kind of works, but I know that there are some mistakes which I am fixing...
What I want to know is this: How do I replace a letter in the array with another letter that is already stored in the array? This is not what we were asked but i'm wondering how you would go about implementing such a thing.
Example: User enters Hello world, replace the 7th word in the array with the first word entered.
Last edited by Ancient Dragon; Apr 16th, 2008 at 10:36 pm. Reason: add line numbers
line 9: If you want someone to type up to 20 characers then you have to define charString as 21 in order to allow room for the null string terminating character.
line 23: All you want here is to enter a single character, not an entire string. Similar to line 26.
lines 29-42: all you need here is just one loop to test each character in sentenceHolder1 for the character you enter on line 23 and possibly replace it with the replacement character.
line 23: All you want here is to enter a single character, not an entire string. Similar to line 26.
lines 29-42: all you need here is just one loop to test each character in sentenceHolder1 for the character you enter on line 23 and possibly replace it with the replacement character.
C++ Syntax (Toggle Plain Text)
int len = strlen(sentenceHolder1); for(int i = 0; i < len; i++) { if( sentenceHolder1[i] == look_for_character ) sentenceHolder1[i] = replacement_character; }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Stick to the program requirements and don't try to do fancy stuff. Your task is to code what the prof asked you to code, not to make up stuff on your own.
There isn't anything wrong with trying out various things on your own -- just don't hand any of it in as part of the assignment.
There isn't anything wrong with trying out various things on your own -- just don't hand any of it in as part of the assignment.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Feb 2008
Posts: 17
Reputation:
Solved Threads: 0
•
•
•
•
Stick to the program requirements and don't try to do fancy stuff. Your task is to code what the prof asked you to code, not to make up stuff on your own.
There isn't anything wrong with trying out various things on your own -- just don't hand any of it in as part of the assignment.
•
•
•
•
Any help with how to replace an instance of a character in the array with the first character of the array would be greatly appreciated
sentenceHolder1[i] = sentenceHolder2[0]; will replace a character in sentenceHolder1 with the first charcter in sentenceHolder2.>>Yea I wasn't planning on handing that part in, my prof just suggested in one of our classes that it
>>was possible and I was wondering how you could do it because he won't tell me.
Yes it is possible. Hint: strstr() from string.h is your friend when working with charcter arrays. There are better ways when using c++ std::string class, but you probably have not gotton to those in your class.
Last edited by Ancient Dragon; Apr 16th, 2008 at 11:12 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Mar 2008
Posts: 42
Reputation:
Solved Threads: 6
•
•
•
•
Thanks for that. As I said I know that there are several errors in this code, thanks for helping fix that one.
Any help with how to replace an instance of a character in the array with the first character of the array would be greatly appreciated
C++ Syntax (Toggle Plain Text)
int len = strlen(sentenceHolder1); for(int i = 0; i < len; i++) { if( sentenceHolder1[i] == look_for_character ) sentenceHolder1[i] = sentenceHolder1[0]; //I think this will be OK }
maybe, the following code will help you.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> using namespace std; int main(int argc, char *argv[]) { char sentenceHolder1[128]; char toReplace; int index1; char replacement; char exitOnInput; cout << "Enter a sentence: " << endl; cin.getline(sentenceHolder1, sizeof(sentenceHolder1) ); cout << "Enter a character that you would like to be replaced" << endl; cin >> toReplace; cout << "Enter what you would like it to be replaced with" << endl; cin >> replacement; for(index1=0; index1 < strlen(sentenceHolder1); index1++) { if (sentenceHolder1[index1] == toReplace) { sentenceHolder1[index1] = replacement; } } cout<<sentenceHolder1<<endl; cout << "Enter a letter followed by 'enter' to exit" << endl; cin >> exitOnInput; return 0; }
That's the same as I posted, but this:
for(index1=0; index1 < strlen(sentenceHolder1); index1++) You don't want to call strlen() like that because it has to be executed on every iteration of the loop. Code it once as I did and just use that variable. Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Safe Array (C++)
- string manipulation (C)
- Simple string manipulation (C)
Other Threads in the C++ Forum
- Previous Thread: Strange concurrent array overwrite
- Next Thread: Two-dimensional array problem
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






