C++ 循环向后打印字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13185736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:03:29  来源:igfitidea点击:

Loop to print string backwards

c++string

提问by user1780064

Program asks user for a series of strings (their name and an 8 letter word), prints their name, the first and last three letters of the word, and then prints their word backwards. need help with the for loop to display string backwards.

程序要求用户输入一系列字符串(他们的名字和一个 8 个字母的单词),打印他们的名字,单词的第一个和最后三个字母,然后向后打印他们的单词。需要帮助 for 循环向后显示字符串。

    #include <iostream> 

int main () { 


string FirstName; 

string LastName; 

string MiddleName; 

string Names; 

string string1; 

int len; 

int x;  

 cout << "Hello. What is your first name?" << endl; 

 cin >> FirstName; 

 cout << FirstName  << ", what is your last name?" << endl; 

 cin >> LastName; 

 cout << "And your middle name?" << endl; 

 cin >> MiddleName; 

 Names = LastName + ", " + FirstName + ", " + MiddleName; 

 cout << Names << endl; 

 cout << "Please enter a word with 8 or more characters (no spaces): " << endl; 

 cin >> string1; 

 len = string1.length(); 

   if (len < 8){
     cout << "Error. Please enter a word with 8 or more characters and no spaces: " <<    endl; 

     cin >> string1; 
 }

  else if (len >= 8){

     cout << "The word you entered has " << string1.length() << " characters."<<endl; 

 cout << "The first three characters are " << string1.substr(0,3) << endl; 

 cout << "The last three characters are " <<string1.substr(string1.length()-3,3) << endl; 

x = string1.length()-1; 

for (x = string1.length()-1; x >=0; x--){
 cout << "Your word backwards: " << string1[x]; 
}
}



return 0; 
} 

回答by alestanis

You were almost there:

你快到了:

cout << "Your word backwards: ";
for (x = string1.length()-1; x >=0; x--){
   cout << string1[x]; 
}

This way the loop will print each character in string1but in reverse order, and the text "Your word backwards: "only once.

这样,循环将以string1相反的顺序打印每个字符,并且文本"Your word backwards: "仅打印一次。

回答by David

If you want to be fancy:

如果你想花哨:

copy(string1.rbegin(), string1.rend(), ostream_iterator<char>(cout));

回答by Dietmar Kühl

This is probably not the answer to you question but I'd one one of these:

这可能不是您问题的答案,但我会选择其中之一:

std::cout << "Your word backwards: "
          << std::string(string1.rbegin(), string1.rend()) << '\n';

*std::copy(string1.rbegin(), string1.rend(),
           std::ostreambuf_iterator<char>(std::cout << "Your word backwards: "))++ = '\n';

std::reverse(string1.begin(), string1.end());
std::cout << "Your word backwards: " << string1 << '\n';

回答by suraj_fale

Simple a way is to store a string in a temp array from backwards and then use this temp array to print reverse string. For eg:- temp[j--]=str[i ++] ; in a loop. But be careful before this , intialise size of array 'temp' to size of original array. in this case 'str' here.

一个简单的方法是从后向临时数组中存储一个字符串,然后使用这个临时数组打印反向字符串。例如:- temp[j--]=str[i ++] ; 在一个循环中。但在此之前要小心,将数组 'temp' 的大小初始化为原始数组的大小。在这种情况下'str'在这里。