我有一个名称为“ Mustang Sally Bob”的字符串
运行代码后,我希望字符串输出像这样:gnatsuM yllaS boB
我的方法是对单词进行计数,直到空格为止,然后保存字符串中空格位置的索引。然后我要从空格开始向后打印字符。
#include <stdio.h>
int main()
{
char* test="Mustang Sally Bob";
int length; //string length
int x;
for(length=0;test[length] !=0&&test[length];length++); //get string length
int counter;
int words = 0;
int space_index =0;
for(counter=0;counter<length;counter++) {
words++;
if(test[counter]==' ') {
space_index=counter;
for(x=space_index-1;x>=words;x--) {
printf("%c",test[x]);
}
words=0;
space_index = 0;
}
}
return 0;
}
但是执行此代码时,我得到的输出是yllaS g
有谁知道为什么我无法获得完整的字符串?