P5705

· · 科技·工程

这一题如果你老老实实用变量做,身为小白的你会炸的.
所以这一题是练习字符串的好机会!
string :

#include<bits/stdc++.h>
using namespace std;
int main(){
    string s1;
/*c++面相对象(class)的字符串,包含在string头文件中(iostream也包含了)*/
    cin>>s1;
/*s1.size()使用来获取字符串长度的,等价于s1.lenth()*/
    for(int i=s1.size()-1;i>=0;i--){
        cout<<s1[i];
    }
    return 0;
}

C风格(字符数组)

#include<bits/stdc++.h>
using namespace std;
int lenth(char *a){
    char *p=a;
    int cnt=0;
    while(*p){
        cnt++;
        p++;
    }
    return cnt;
}
int main(){
    char s[1000];
    scanf("%s",s);
    for(int i=strlen(s)-1;i>=0;i--){
        printf("%c",s[i]);
    }
}