943,723 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 750
  • C++ RSS
Dec 5th, 2008
1

Deleting spaces in a string

Expand Post »
Ok so I'm having some problems with this program I have to write as you might be able to tell it takes strings from the user either in english or morse and translates them to morse or english respectively. I got it working from English -> Morse for strings with no spaces but it terminates the concatonation after a space.
I appreciate whatever help you can give me. Written in C++ in the bloodshed compiler
C++ Syntax (Toggle Plain Text)
  1. //English -> morse code & vice versa 1 space per letter 3 spaces per word
  2. #include<iostream>
  3. #include<cstring>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char Alpha[36] = {'a','b','c','d','e','f','g','h','i','j','k','l', 'm',
  9. 'n','o','p','q','r','s','t','u','v','w','x','y','z',
  10. '1','2','3','4','5','6','7','8','9','0'};
  11. std::string Morse[36] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",
  12. ".---","-.-",".-..","--","-.","---",".--.","--.-",".-.",
  13. "...","-","..-","...-",".--","-..-","-.--","--..",".----",
  14. "..---","...--","....-",".....","-....","--...","---..","----.","-----"};
  15.  
  16. string mphrase;
  17. string ephrase;
  18. int choice = 0;
  19.  
  20. cout<<"Please enter 1 for english to morse code translation or 2 for morse code to english translation. "<<endl;
  21. cin>>choice;
  22. if (choice == 1)
  23. {
  24. cout<<"Enter the phrase you would like to be encoded into morse."<<endl;
  25. cin>>ephrase;
  26. for (int i = 0; i < ephrase.length(); i++)
  27. {
  28. for (int h = 0; h < 36; h++)
  29. {
  30. if (ephrase[i] == ' ')
  31. {
  32. ephrase[i]++;
  33. }
  34.  
  35. else if ( ephrase[i] == Alpha[h] )
  36. {
  37. mphrase += Morse[h];
  38. mphrase += " ";
  39. }
  40. }
  41. }
  42. cout<<mphrase<<endl;
  43. }
  44. /* else if (choice == 2)
  45.   {
  46.   cin>>mphrase;
  47.  
  48.   ephrase += Morse[x];
  49.   }
  50.   else
  51.   {
  52.   cout<<"Please enter a valid response. "<<endl;
  53.   cin>>choice;
  54.   }
  55.   cout<<mphrase;
  56.   */
  57.  
  58. system("pause");
  59. return 0;
  60. }
Similar Threads
Reputation Points: 12
Solved Threads: 0
Newbie Poster
jonevans2 is offline Offline
3 posts
since Dec 2008
Dec 5th, 2008
0

Re: Deleting spaces in a string

Is it a trim function you are looking for?? Deleting the spaces in front of and behind the string is it??
Reputation Points: 11
Solved Threads: 0
Light Poster
Seamus McCarthy is offline Offline
34 posts
since Apr 2008
Dec 5th, 2008
0

Re: Deleting spaces in a string

I can translate a string as long as it doesn't contain a space such as
abcdefg
but when I enter something like
I am cool
it only picks up the I
Reputation Points: 12
Solved Threads: 0
Newbie Poster
jonevans2 is offline Offline
3 posts
since Dec 2008
Dec 5th, 2008
0

Re: Deleting spaces in a string

Sry man cn't figure it out, no the best at programming myself!!
Reputation Points: 11
Solved Threads: 0
Light Poster
Seamus McCarthy is offline Offline
34 posts
since Apr 2008
Dec 5th, 2008
0

Re: Deleting spaces in a string

referring to the topic and omitting the necessary namespace and #includes:

C++ Syntax (Toggle Plain Text)
  1. string str("This is a test");
  2. str.erase(remove_if(str.begin(), str.end(), bind2nd(equal_to<char>(), ' ')), str.end());
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Dec 5th, 2008
0

Re: Deleting spaces in a string

You should get ephrase with getline(cin, ephrase); instead of cin >> ephrase; .

Before doing that, though, you should flush the input stream (great thread on this by Narue) because of the previous cin >> choice; .

Also, your program would not translate "I am cool" as long as you don't convert to lowercase the I (same story for any uppercase letter).
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Dec 5th, 2008
0

Re: Deleting spaces in a string

C++ Syntax (Toggle Plain Text)
  1. if (ephrase[i] == ' ')
  2. {
  3. ephrase[i]++;
  4. }

Do you really really want to increment the value of the char at ephrase[i]?
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Dec 5th, 2008
0

Re: Deleting spaces in a string

in regards to all the posts jencas first post didn't work if I recieved a string from the user it would work if it was already assigned in the program. In regards to the 2nd comment
c++ Syntax (Toggle Plain Text)
  1. if (ephrase[i] == ' ')
  2. {
  3. ephrase[i]=ephrase[i+1];
  4. }
I changed that up but still no results. Mr. I tried getline but I got nothing but I have not tried flushing the stream so I'm in the process of reading that now. I think I might be making this program more complicated than it seems though because I am supposed to be using the string tokenizer along with other string functions.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
jonevans2 is offline Offline
3 posts
since Dec 2008
Dec 5th, 2008
0

Re: Deleting spaces in a string

Try:

C++ Syntax (Toggle Plain Text)
  1. for (int i = 0; i < ephrase.length(); i++)
  2. {
  3. if (ephrase[i] == ' ')
  4. continue;
  5.  
  6. for (int h = 0; h < 36; h++)
  7. {
  8. if ( ephrase[i] == Alpha[h] )
  9. {
  10. mphrase += Morse[h];
  11. mphrase += " ";
  12. }
  13. }
  14. }

Don't even try to shift part of the string, if you don't know how to do it
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Dec 5th, 2008
0

Re: Deleting spaces in a string

Can't you simplify Alpha, and Morse like this:
string Alpha = "abcd...7890"; vector<string> Morse; You probably could come up with an algorithm to push_back() all the elements, for Morse. Or for now, do it manually for the first few ones, just to test it out.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008

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: How to Initialize COM Library in VC++.NET 2005
Next Thread in C++ Forum Timeline: Newbie on Minifilter Driver Dev





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


Follow us on Twitter


© 2011 DaniWeb® LLC