我的代码在下面有问题,我想颠倒一个字符串,但是我运行时错误,谁能帮我检查一下?问题是:
如:用C语言逐字颠倒一个字符串的字
输入:字符* S = “这是我的字符串”
OUTPUT: “字符串我是这样的”
#include <iostream>
using namespace std;
void reverse(char *str, int start, int end){
char tmp;
while(end > start){
tmp = str[end];
str[end] = str[start];
str[start] = tmp;
end--;
start++;
}
}
int main()
{
char *s = "This is my string";
int len = strlen(s);
int start = 0;
int end = len-1;
reverse(s, start, end);
printf("%s", s);
end = 0;
while(end < len){
if(s[end] == ' '||s[end] =='\0'){
while(s[start]==' ')
start++;
reverse(s,start,end-1);
start = end;
}
end++;
}
printf("%s", s);
cin.get();
}
你的代码似乎不是使用C语言,而是使用C++。 – heretolearn
你的'main'函数应该返回一个整数。这与你的问题无关,但是很好的编码习惯。标准头文件stdlib.h定义了常量“EXIT_SUCCESS”和“EXIT_FAILURE”,它们是从main('EXIT_SUCCESS'成功完成返回,否则返回'EXIT_FAILURE')的良好默认值。 – sfstewman