Deleting spaces in a string

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 3
Reputation: jonevans2 is an unknown quantity at this point 
Solved Threads: 0
jonevans2 jonevans2 is offline Offline
Newbie Poster

Deleting spaces in a string

 
1
  #1
Dec 5th, 2008
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
  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 34
Reputation: Seamus McCarthy is an unknown quantity at this point 
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Light Poster

Re: Deleting spaces in a string

 
0
  #2
Dec 5th, 2008
Is it a trim function you are looking for?? Deleting the spaces in front of and behind the string is it??
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: jonevans2 is an unknown quantity at this point 
Solved Threads: 0
jonevans2 jonevans2 is offline Offline
Newbie Poster

Re: Deleting spaces in a string

 
0
  #3
Dec 5th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 34
Reputation: Seamus McCarthy is an unknown quantity at this point 
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Light Poster

Re: Deleting spaces in a string

 
0
  #4
Dec 5th, 2008
Sry man cn't figure it out, no the best at programming myself!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: Deleting spaces in a string

 
0
  #5
Dec 5th, 2008
referring to the topic and omitting the necessary namespace and #includes:

  1. string str("This is a test");
  2. str.erase(remove_if(str.begin(), str.end(), bind2nd(equal_to<char>(), ' ')), str.end());
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Deleting spaces in a string

 
0
  #6
Dec 5th, 2008
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).
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: Deleting spaces in a string

 
0
  #7
Dec 5th, 2008
  1. if (ephrase[i] == ' ')
  2. {
  3. ephrase[i]++;
  4. }

Do you really really want to increment the value of the char at ephrase[i]?
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 3
Reputation: jonevans2 is an unknown quantity at this point 
Solved Threads: 0
jonevans2 jonevans2 is offline Offline
Newbie Poster

Re: Deleting spaces in a string

 
0
  #8
Dec 5th, 2008
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
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: Deleting spaces in a string

 
0
  #9
Dec 5th, 2008
Try:

  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
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 964
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Deleting spaces in a string

 
0
  #10
Dec 5th, 2008
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.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC