我與我的倒車數字1和2反轉字符串C++無反向功能
任何的字符串可以相應地通知有問題嗎?
我想通過tempholder1和tempholder2作爲函數變量並通過for循環來反轉。
我cout << digit1 << digit2; // but do not get anything at the compiler.
編輯,以便提高效率
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
/* GLOBAL DECLARATIONS */
/* FUNCTIONS */
// Reverse the string
void reverseString(string&);
// Transfer all the digits in a char array to an integer array
// Add two long integers
// Print out the long integers
int main()
{
string digit1, digit2;
char again; // Y for menu
do
{
// Fetch Values
cout << "Enter a string of digits: ";
cin >> digit1;
cout << "Enter a string of digits: ";
cin >> digit2;
// Reverse the string
reverseString(digit1);
reverseString(digit2);
cout << digit1 << digit2;
// Return sum of both values
cout << "The sum is: ";
// Do it again?
cout << "Continue (Y/N)?: ";
cin >> again;
} while ((again=='y')||(again=='Y'));
}
// Reverse the string
void reverseString(string& digit)
{
string tempholder1;
// tempholder1 = reverse of digit1
int k=0;
for(int i=digit.length()-1;i>=0;i--)
{
tempholder1[k]=digit[i];
k++;
}
digit=tempholder1;
}
爲什麼reverseString反向兩個字符串,而不是它扭轉一個字符串,然後調用它兩次? –
@TimB我用string&digit1,string&digit2(通過引用傳遞)和void語句。所以它應該罰款2在同一時間 –
但它仍然是一個壞主意。你已經重複了代碼,並且使得該函數不太常用。 (如果你想要反轉1個字符串,或者3?) –