A separate sequence of identical numbers from a string

I am trying to do a small task that asks to convert numbers to letters on the telephone keypad, for example, if input number 222 means the telephone button "2" ( http://upload.wikimedia.org/wikipedia/commons/7/7d/Telephone-keypad. png ) is popped 3 times, and the output should be "C", etc. So, the first thing I have to do is to separate all the sequences, for example 22255-444, to 222, 55, -, 444, and then I think that everything worked out, but now the problem is that my function cannot read the last sequence

#include <iostream> #include <fstream> using namespace std; //------------------------------------------------------------------------- void encode(string text, string &result, int &i) { char keyboard[10][4] = { {' ',' ',' ',' '}, {'.','?','!','.'}, {'a','b','c','a'}, {'d','e','f','d'}, {'g','h','i','g'}, {'j','k','l','j'}, {'m','n','o','m'}, {'p','r','q','s'}, {'t','u','v','t'}, {'w','x','y','z'} }; int j; for(j = i; j<text.size();j++) { if(text[i] != text[j] || j == text.size()) { result = text.substr(i, ji); i = j-1; break; } } cout << result << endl; } int main() { ifstream fd("sms.in"); string text; string result; getline(fd, text); for(int i = 0; i<text.size();i++) { encode(text, result, i); } return 0; } 

as a test now using this input: 5552-22-27777, the output should be 555 2 - 22 - 2 7777, but for me its 555 2 - 22 - 2 2 2 2 2 2.

+6
source share
2 answers

In this if :

 if(text[i] != text[j] || j == text.size()) 

The second condition ( j == text.size() ) will never be true, because the loop will end before that. Therefore, when you reach the end of the line, the values โ€‹โ€‹of result and i will not be updated correctly.

What you can do is remove the termination condition from the loop (this is not necessary because you exit the loop anyway). And you will need to reverse the order of the conditions in if so you don't read beyond the end of the line:

 for(j = i; ;j++) { if (j == text.size() || text[i] != text[j]) ... 
+2
source

I would pull the last bit from the for loop, in fact this is not so, because this is a special case. I also eliminated some other things that seemed unnecessary and fixed the inside of the loop. See if all this makes sense to you.

I pulled out a keymap declaration because it was not used in your example, and we value what is called โ€œminimal working examplesโ€ here as a way to isolate and discuss issues convincingly.

Please note that I tried to move all the coding work into one function and from the main () function. This frees up the main part for processing the higher-level parts of the program. On the lines that I designated "Decode here", you can call another function that translates a string of numbers into a character, if that is what you are going to do.

I noted a special case and explicitly pulled it out of the for loop, so that everything inside the for loop can be handled the same. This is better than trying to get the for-loop to do everything.

I disagree with the logic that i=j-1 , so I changed this (I experienced this).

I also switched to reading input directly from the command line to test faster.

I added the #include <string> directive at the beginning so that the program compiles.

Streamlining all coding work within a single function has simplified the declaration of functions into a single argument.

I switched the brackets to fit my own understanding of One True Style.

 #include <iostream> #include <fstream> #include <string> using namespace std; void encode(string text) { int j=0,i=0; //Declare j here so we can use in special case for(j=0; j<text.size(); j++){ if(text[j] != text[i]){ cout << text.substr(i, ji) << endl; //Decode here i = j; } } cout << text.substr(i,j-1) << endl; //Decode here. Special case } int main(){ string text; getline(cin, text); encode(text, result); return 0; } 
0
source

All Articles