请问我哪错了
是不是文件没有设置成UT8的编码,或者编码不统一
https://jingyan.baidu.com/article/67508eb4199ea49cca1ce497.html
看这个,你就懂了,希望能帮到你
void RS(char *bp, char *ep)
{
while(bp < ep)
{
char tem = *bp;
*bp = *ep;
*ep = tem;
bp++;
ep--;
}
}
void Reverse(char *s)
{
int len = strlen(s);
char *es = s + len -1;
RS(s,es);
char *p1 = s;
char *p2 = s;
while( *p2 != '\0')
{
while(*p2 != '\0' && *p2 != ' ')
p2++;
RS(p1,p2-1);
if( *p2 == ' ' && *p2 != '\0')
{
p2++;
p1 = p2;
}
}
}
char test[] = "Hello World Here I Come";
Reverse(test);
for(i = 0; i < cd; i++){
if(a[i] == ' '){
b[num][flag] = '\0';
flag = 0;
num++;
continue;
}
b[num][flag] = a[i];
flag++;
}
b[num][flag] = '\0';
for(i = num; i >= 0; i--){
printf("%s ", b[i]);
}
一看见烫字,十有八九字符串没有以\0结尾,代码我不看了,你去查查,基本跑不了。
首先楼主这里有个问题:
for(i=num-1;i>=0;i--)这里不需要减一。随便带个数字进去看看就知道了直接i=num
其次
楼主的问题很明显,野指针问题。
我们看输入AB_CD后
A保存在b[0][0]中
B保存在b[0][1]中
C保存在b[1][0]中
D保存在b[1][1]中
接下来输出b[i]是有很大问题的
就我的AB_CD例子
打印b[0]输出是CD AB后面一堆乱码
打印b[1]输出是AB后面一堆乱码
因为没有终止符号'\0',数组后面又没有内容(野指针问题),强行读取必然乱码。
建议用string,
string s;
string s_temp;
while(cin >> s_temp)
{
string s_temp;
for(auto c : s)
s_temp += c;
s += (s_temp + " ");
}
结尾用\0试试,差不了
英语句子按单词倒序输出C语言版
结束符没有肯定会有乱码,但是现象应该是 H烫烫烫
而且你的格式也不对,起码给个/t作为空格
printf("%s\t",b[num])