Invertir una cadena - c ++
Frecuentes
Visto 169 veces
-2
I'm polishing up on my C++ and taking an exercise on reversing a user entered string. Below is my attempt which isn't working.
¿Alguna idea de por qué?
string userentry;
cout << "enter a string: ";
getline(cin, userentry);
int size = userentry.length();
for (int i = 0; i < size /2; i++)
{
string tempvar1 = userentry.substr(i);
string tempvar2 = userentry.substr(size - 1 - i);
userentry.replace(i, i+1, tempvar2);
userentry.replace(size - 1 -i, (size - 1 - i) + 1, tempvar1);
}
cout << userentry << endl;
return 0;
Agradezco cualquier ayuda.
2 Respuestas
0
Essentially, you're inserting a growing string into itself overwriting an increasing range of characters. I presume you intended to replace one character at a time.
Try setting the length argument of replace() to 1, and only specify the first character of the temp variables. For example:
for (int i = 0; i < size /2; i++)
{
string tempvar1 = userentry.substr(i);
string tempvar2 = userentry.substr(size - 1 - i);
userentry.replace(i, 1, tempvar2.substr(0, 1));
userentry.replace(size - 1 -i, 1, tempvar1.substr(0, 1));
}
Here's the results showing your growing string inserts:
enter a string: abcdef
Step 1
t1: abcdef
t2: f
ue: abcdef
first replace at 0, length 1 on data: abcdef, using: f
second replace at 5, length 6 on data: fbcdef, using: abcdef
Step 2
t1: bcdeabcdef
t2: eabcdef
ue: fbcdeabcdef
first replace at 1, length 2 on data: fbcdeabcdef, using: eabcdef
second replace at 4, length 5 on data: feabcdefcdeabcdef, using: bcdeabcdef
Step 3
t1: abbcdeabcdefdeabcdef
t2: bbcdeabcdefdeabcdef
ue: feabbcdeabcdefdeabcdef
first replace at 2, length 3 on data: feabbcdeabcdefdeabcdef, using: bbcdeabcdefdeabcdef
second replace at 3, length 4 on data: febbcdeabcdefdeabcdefbbcdeabcdefdeabcdef, using: abbcdeabcdefdeabcdef
results: febabbcdeabcdefdeabcdefabcdefdeabcdefbbcdeabcdefdeabcdef
respondido 27 nov., 13:06
-1
well, I'm not sure how the one you have is supposed to work but I've made one that does.
string userentry;
cout << "enter a string: ";
getline(cin, userentry);
int size = userentry.length();
string temp = "";//construscts a temporary string
for (int i = size-1; i >=0; i--){//we want to start at the end of the inputted string because that will be placed at the beggining of the temporary string
temp.push_back(userentry[i]);//adds the last character to userentry
}
userentry = temp;//assign userentry to its reversed value
cout << userentry << endl;
return 0;
respondido 27 nov., 13:06
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas c++ or haz tu propia pregunta.
Describe what you believe every line does. If the program does not do what you think it does then one of the descriptions is wrong. Step through the code in the debugger and verify that every description matches the actual behaviour; the one that doesn't is the bug. - Eric Lippert
@Blastfurnace: The aim of the exercise isn't to learn how to call someone else's string reverse algorithm. I doubt the original poster has a pressing need to reverse a string. - Eric Lippert
If we stick to the level of detail you provided when describing the problem ("isn't working"), we would have to answer, "because you apparently made a mistake somewhere"... - Spook
You can treat the string as an array of chars. So, find the indices or iterators to the first and last elements. Swap the elements. Increment one index/iterator, decrement the other. repeat until you reach the halfway point. - juanchopanza