String manipulation as array of char

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 17
Reputation: cpp_noobsauce is an unknown quantity at this point 
Solved Threads: 0
cpp_noobsauce cpp_noobsauce is offline Offline
Newbie Poster

String manipulation as array of char

 
0
  #1
Apr 16th, 2008
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:
  1.  
  2. #include <iostream>
  3. #include <cstring>
  4.  
  5.  
  6. void main(void)
  7. {
  8.  
  9. const int charString = 20;
  10. char sentenceHolder1[charString];
  11. char sentenceHolder2[charString];
  12. int index1;
  13. int index2;
  14. int index3;
  15. char replacement;
  16. char exitOnInput;
  17. int count = 0;
  18.  
  19. cout << "Enter a sentence of up to 20 characters" << endl;
  20. cin.getline(sentenceHolder1, charString);
  21.  
  22. cout << "Enter a character that you would like to be replaced" << endl;
  23. cin.getline(sentenceHolder2, charString);
  24.  
  25. cout << "Enter what you would like it to be replaced with" << endl;
  26. cin >> replacement;
  27.  
  28.  
  29. for(index1=0; index1 < charString; index1++)
  30. {
  31. for(index2=0; index2 < charString; index2++)
  32. {
  33.  
  34. while ( sentenceHolder1[index1] == sentenceHolder2[index2] )
  35. {
  36. count = count+1;
  37.  
  38. sentenceHolder1[index1] = replacement;
  39.  
  40.  
  41. }
  42. }}
  43.  
  44. for ( index3 = 0; index3 < charString; index3++)
  45. {
  46. cout << sentenceHolder1[index3];
  47.  
  48. }
  49. cout << "Enter a letter followed by 'enter' to exit" << endl;
  50. cin >> exitOnInput;
  51. return 0;
  52.  
  53. }

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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: cpp_noobsauce is an unknown quantity at this point 
Solved Threads: 0
cpp_noobsauce cpp_noobsauce is offline Offline
Newbie Poster

Re: String manipulation as array of char

 
0
  #2
Apr 16th, 2008
Better yet - how to replace all instances of a certain letter in the array ( lets say L ) with the first letter in the array.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,625
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1495
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: String manipulation as array of char

 
0
  #3
Apr 16th, 2008
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.
  1. int len = strlen(sentenceHolder1);
  2. for(int i = 0; i < len; i++)
  3. {
  4. if( sentenceHolder1[i] == look_for_character )
  5. sentenceHolder1[i] = replacement_character;
  6. }
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,625
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1495
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: String manipulation as array of char

 
0
  #4
Apr 16th, 2008
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: cpp_noobsauce is an unknown quantity at this point 
Solved Threads: 0
cpp_noobsauce cpp_noobsauce is offline Offline
Newbie Poster

Re: String manipulation as array of char

 
0
  #5
Apr 16th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: cpp_noobsauce is an unknown quantity at this point 
Solved Threads: 0
cpp_noobsauce cpp_noobsauce is offline Offline
Newbie Poster

Re: String manipulation as array of char

 
0
  #6
Apr 16th, 2008
Originally Posted by Ancient Dragon View Post
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,625
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1495
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: String manipulation as array of char

 
-1
  #7
Apr 16th, 2008
Originally Posted by cpp_noobsauce View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: cpp_noobsauce is an unknown quantity at this point 
Solved Threads: 0
cpp_noobsauce cpp_noobsauce is offline Offline
Newbie Poster

Re: String manipulation as array of char

 
0
  #8
Apr 16th, 2008
Thanks!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: littlestone is an unknown quantity at this point 
Solved Threads: 6
littlestone littlestone is offline Offline
Light Poster

Re: String manipulation as array of char

 
1
  #9
Apr 16th, 2008
Originally Posted by cpp_noobsauce View Post
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
  1. int len = strlen(sentenceHolder1);
  2. for(int i = 0; i < len; i++)
  3. {
  4. if( sentenceHolder1[i] == look_for_character )
  5. sentenceHolder1[i] = sentenceHolder1[0]; //I think this will be OK
  6. }

maybe, the following code will help you.

  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.  
  9. char sentenceHolder1[128];
  10. char toReplace;
  11. int index1;
  12. char replacement;
  13. char exitOnInput;
  14.  
  15. cout << "Enter a sentence: " << endl;
  16. cin.getline(sentenceHolder1, sizeof(sentenceHolder1) );
  17.  
  18. cout << "Enter a character that you would like to be replaced" << endl;
  19. cin >> toReplace;
  20.  
  21. cout << "Enter what you would like it to be replaced with" << endl;
  22. cin >> replacement;
  23.  
  24.  
  25. for(index1=0; index1 < strlen(sentenceHolder1); index1++)
  26. {
  27. if (sentenceHolder1[index1] == toReplace)
  28. {
  29. sentenceHolder1[index1] = replacement;
  30. }
  31. }
  32.  
  33. cout<<sentenceHolder1<<endl;
  34.  
  35. cout << "Enter a letter followed by 'enter' to exit" << endl;
  36. cin >> exitOnInput;
  37.  
  38. return 0;
  39. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,625
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1495
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: String manipulation as array of char

 
0
  #10
Apr 16th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC