我尝试用c ++执行一个简单的程序,我无法得到那个结果,我现在应该做什么,我在不同平台的gcc编译器上尝试了不同版本的代码.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int i,m,j;
char a[10],b[10],temp;
cout << " give the string " << endl;
cin >> a;
cout << a;
m=strlen(a);
j=0;
for(i=m;i>0;i--){
b[j]=a[i];
cout << " inloop "<<b;
j++;
}
cout << b << endl;
return 0;
Run Code Online (Sandbox Code Playgroud)
}
C中的所有内容都是0索引的.a[i]在第一次迭代是a[strlen(a)]哪个\0.
如果输入是bobo,则数组的内容a将是
a[0] = 'b'
a[1] = 'o'
a[2] = 'b'
a[3] = 'o'
a[4] = '\0'
Run Code Online (Sandbox Code Playgroud)
你的循环从[4]开始(因为strlen(a)== 4),所以你的b字符串将是:
b[0] = '\0'
b[1] = 'o'
b[2] = 'b'
b[3] = 'o'
b[4] = 'b'
Run Code Online (Sandbox Code Playgroud)
打印它将导致""被打印.
| 归档时间: |
|
| 查看次数: |
696 次 |
| 最近记录: |