C++的问题,iteration和arrays
来源:百度知道 编辑:UC知道 时间:2024/09/20 22:28:55
假设C-style string(字符数组)就像这样的:
char message[] = "This is a message.";
编写C++代码来写出字符串向后(".egassem a si sihT").
原题是:
assuming a C-style string (character array) to have been declared like this:
char message[] = "This is a message.";
write C++ code to print the string out backwards (".egassem a si sihT").
我应该没翻译错!
char message[] = "This is a message.";
编写C++代码来写出字符串向后(".egassem a si sihT").
原题是:
assuming a C-style string (character array) to have been declared like this:
char message[] = "This is a message.";
write C++ code to print the string out backwards (".egassem a si sihT").
我应该没翻译错!
//可以用C的方法实现,但C++里提倡使用C++类库
#include <iostream>
#include <string>
using namespace std;
int main()
{
char message[] = "This is a message.";
string str = message;
for(int i = str.size()-1; i >= 0; i--)
cout << str[i];
return 0;
}
#include <string.h>
#include<iostream>
using namespace std;
int main()
{
char message[] = "This is a message.";
strrev(message);
cout << message;
return 0;
}
#include<iostream>
using namespace std;
char Show(const char*c);
int main()
{
char message[] = "This is a message. ";
Show(message);
return 0;
}
char Show(const char* c)
{
if( *c == '\0')
return *c;
else{
cout <<Show(++c);
}
}
#include<iostream>