| | |
Deleting spaces in a string
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
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
I appreciate whatever help you can give me. Written in C++ in the bloodshed compiler
C++ Syntax (Toggle Plain Text)
//English -> morse code & vice versa 1 space per letter 3 spaces per word #include<iostream> #include<cstring> using namespace std; int main() { char Alpha[36] = {'a','b','c','d','e','f','g','h','i','j','k','l', 'm', 'n','o','p','q','r','s','t','u','v','w','x','y','z', '1','2','3','4','5','6','7','8','9','0'}; std::string Morse[36] = {".-","-...","-.-.","-..",".","..-.","--.","....","..", ".---","-.-",".-..","--","-.","---",".--.","--.-",".-.", "...","-","..-","...-",".--","-..-","-.--","--..",".----", "..---","...--","....-",".....","-....","--...","---..","----.","-----"}; string mphrase; string ephrase; int choice = 0; cout<<"Please enter 1 for english to morse code translation or 2 for morse code to english translation. "<<endl; cin>>choice; if (choice == 1) { cout<<"Enter the phrase you would like to be encoded into morse."<<endl; cin>>ephrase; for (int i = 0; i < ephrase.length(); i++) { for (int h = 0; h < 36; h++) { if (ephrase[i] == ' ') { ephrase[i]++; } else if ( ephrase[i] == Alpha[h] ) { mphrase += Morse[h]; mphrase += " "; } } } cout<<mphrase<<endl; } /* else if (choice == 2) { cin>>mphrase; ephrase += Morse[x]; } else { cout<<"Please enter a valid response. "<<endl; cin>>choice; } cout<<mphrase; */ system("pause"); return 0; }
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
referring to the topic and omitting the necessary namespace and #includes:
C++ Syntax (Toggle Plain Text)
string str("This is a test"); 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
Please use code tags - Please mark solved threads as solved
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
You should get ephrase with
Before doing that, though, you should flush the input stream (great thread on this by Narue) because of the previous
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).
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).
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
C++ Syntax (Toggle Plain Text)
if (ephrase[i] == ' ') { ephrase[i]++; }
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
Please use code tags - Please mark solved threads as solved
•
•
Join Date: Dec 2008
Posts: 3
Reputation:
Solved Threads: 0
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 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.
c++ Syntax (Toggle Plain Text)
if (ephrase[i] == ' ') { ephrase[i]=ephrase[i+1]; }
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
Try:
Don't even try to shift part of the string, if you don't know how to do it
C++ Syntax (Toggle Plain Text)
for (int i = 0; i < ephrase.length(); i++) { if (ephrase[i] == ' ') continue; for (int h = 0; h < 36; h++) { if ( ephrase[i] == Alpha[h] ) { mphrase += Morse[h]; mphrase += " "; } } }
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
Please use code tags - Please mark solved threads as solved
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
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
![]() |
Similar Threads
- More than one condition with getline? (C++)
- I come in Peace (Community Introductions)
- help me please (JavaScript / DHTML / AJAX)
- deleting spaces in an ansistring (C++)
- help with shell script padding files with spaces (Shell Scripting)
Other Threads in the C++ Forum
- Previous Thread: How to Initialize COM Library in VC++.NET 2005
- Next Thread: Newbie on Minifilter Driver Dev
| Thread Tools | Search this Thread |
api array arrays 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 download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





