943,949 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4455
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 16th, 2008
0

String manipulation as array of char

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cpp_noobsauce is offline Offline
17 posts
since Feb 2008
Apr 16th, 2008
0

Re: String manipulation as array of char

Better yet - how to replace all instances of a certain letter in the array ( lets say L ) with the first letter in the array.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cpp_noobsauce is offline Offline
17 posts
since Feb 2008
Apr 16th, 2008
0

Re: String manipulation as array of char

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.
C++ Syntax (Toggle Plain Text)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 16th, 2008
0

Re: String manipulation as array of char

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 16th, 2008
0

Re: String manipulation as array of char

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cpp_noobsauce is offline Offline
17 posts
since Feb 2008
Apr 16th, 2008
0

Re: String manipulation as array of char

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cpp_noobsauce is offline Offline
17 posts
since Feb 2008
Apr 16th, 2008
-1

Re: String manipulation as array of char

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 16th, 2008
0

Re: String manipulation as array of char

Thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cpp_noobsauce is offline Offline
17 posts
since Feb 2008
Apr 16th, 2008
1

Re: String manipulation as array of char

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)
  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.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 14
Solved Threads: 6
Light Poster
littlestone is offline Offline
42 posts
since Mar 2008
Apr 16th, 2008
0

Re: String manipulation as array of char

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: [c++ builder 6] multiline listbox?
Next Thread in C++ Forum Timeline: Who's good with inheritance looping?





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


Follow us on Twitter


© 2011 DaniWeb® LLC