String 代码块超时:程序反转字符串(例如-ab cd将反转为ba dc)
下面是给出错误超时的程序的示例 同样的程序:String 代码块超时:程序反转字符串(例如-ab cd将反转为ba dc),string,timeout,codeblocks,String,Timeout,Codeblocks,下面是给出错误超时的程序的示例 同样的程序: void reverse(char a[],int slen, int elen) { int start,end,temp; for(start=slen,end=elen;start<end;start++,end--) { temp=a[start]; a[start]=a[end]; a[end]
void reverse(char a[],int slen, int elen)
{
int start,end,temp;
for(start=slen,end=elen;start<end;start++,end--)
{
temp=a[start];
a[start]=a[end];
a[end]=temp;
}
}
void reverseall(char a[])
{
int i,n=0,len=0;
for(i=0;i<20;i++)
while(a[i]!='\0')
n++;
for(i=0;i<n;i++)
{
while(a[i]!= ' ')
len++;
reverse(a,i-len,i-1);
}
for(i=0;i<n;i++)
cout<<a[i];
}
int main()
{
char a[20]="hi how are you";
reverseall(a);
return 0;
}
void reverse(字符a[],整数长度,整数长度)
{
int开始、结束、温度;
for(start=slen,end=elen;start递增n和len的两个while循环都在检查a[i]是'\0'还是'',而不改变循环体中的i。它们应该检查a[n]还是a[len]是您想要的,因为n和len是循环中递增的值。因为结束每个循环的条件在循环中没有改变,它们都将永远运行。@Shelam Oh ya明白了。非常感谢!我做了更改,但仍然得到相同的错误。我在纸上计算出来,似乎找不到错误:(--链接到新程序您的while循环while(a[j]!=''a[j]!='\0')将永远不会退出,因为这两个条件中的一个将始终为真…如果a[j]=''和a[j]='\0',它们都可能为假