This is a program I wrote for a homework problem. It said a list of telephone numbers had been reversed and we need to write a program to switch them back.
My professor included this hint in the instructions: Hint: One way to reverse the number would be to accept all the input as strings, create a temporary string of length 10,
and then manually change each character of the string (e.g. temp.at(2)=number.at(8); etc.)
I tried doing something like that but it isn't working. Here is what I have:
The quickest way is to just print each char in the string in a for loop. Luckily, in C++ the string class is pretty amazing. But in C it would be the same since all strings are just char arrays. You would just have to keep track of the size like with any array.
Read up on the string class and actually learn why this works. Try to understand why you type #include <string> in your code because there are many other methods you can use to solve problems.
1 2 3 4 5 6 7 8
string ReverseString(string word)
{
string tmp;
for (int i = word.size(); i > 0; i--)
tmp += word[i];
return tmp;
}
Make this a function that you pass each string into. Just use a simple loop to call the function with entry.
@IceThatJaw - your code overflows at its first iteration. strings are indexed from zero through to size() - 1, and your loop starts with int i = word.size()
I'm not really understanding any of this. @IceThatJaw when I included your code in mine the output file just said "dro". I'm assuming it took "word" and reversed it and somehow lost the w. But I need the program to reverse a list of phone numbers.