c++程序求解错误在哪?

2024-12-02 09:12:38
推荐回答(3个)
回答1:

SIZE的定义缺少类型
2个函数的定义逻辑上有问题 会抛出越界的异常 for()语句的地方 楼主可以看下
自己编写的函数调用有问题 guolv()参数是数组地址 而不是数组元素(楼主的写法不仅越界而且还是数组元素的传递)
主函数的没有返回值
我贴了一个代码在下面 楼主可以看下 可以的话楼主的编码风格改一下 可能更符合C++
#include
#include
using namespace std;
const int SIZE=100;

int guolv(char input[],int a,char mid[]);
int exchange(char exchange[],int a);

int guolv(char input[],int a,char mid[]){int i,j;
j=0;
for(i=0;i {
if((input[i]<='z'&&input[i]>='a')||(input[i]<='Z'&&input[i]>='A'))
mid[j]=input[i];j++;
return j;
}
}
int exchange(char exchange[],int a)
{
int i;
for(i=0;i {
if( exchange[i]<='Z'&&exchange[i]>='A')exchange[i]-=32;
return 0;
}
}

void main()
{
char carray[SIZE];char Mid[SIZE];
int i,len,is_palindrome=1;
int j;
cout<<"Please input a string:\n";
cin.get(carray,SIZE);
len=strlen(carray);
j=guolv(carray,len,Mid);
exchange(Mid,j);

for(i=0;i {
if(carray[i]!=carray[len-1-i])
{
is_palindrome=0;
break;
}
}
if(is_palindrome)cout<<"the string is a palindrome\n";
else cout<<"the string is not a palindrome\n";
}

回答2:

#include
#include
using namespace std;
const SIZE=100;

int guolv(char input[],int a,char mid[]);
int exchange(char exchange[],int a);

int guolv(char input[],int a,char mid[])
{int i,j;
j=0;
for(i=0;i<=a;i++)
{
if((input[i]<='z'&&input[i]>='a')||(input[i]<='Z'&&input[i]>='A'))
mid[j]=input[i];j++;
return j;
}
}
int exchange(char exchange[],int a){
int i;
for(i=0;i<=a;i++){
if( exchange[i]<='Z'&&exchange[i]>='A')exchange[i]-=32;
return 0;
}}

void main(){
char carray[SIZE];char Mid[SIZE];
int i,len,is_palindrome=1;
int j;
cout<<"Please input a string:\n";
cin.get(carray,SIZE);
len=strlen(carray);//carray,多打了一个r
j=guolv(carray,len,Mid);//去掉[size],不然就把carry[100]那个值带入了
exchange(Mid,j);//同上

for(i=0;i if(carray[i]!=carray[len-1-i]){
is_palindrome=0;
break;}
}
if(is_palindrome)cout<<"the string is a palingrome\n";
else cout<<"the string is not a palindrome\n";
}//void 没有返回值

回答3:

if(is_palindrome)cout<<"the string is a palingrome\n";
else cout<<"the string is not a palindrome\n";
return 0;}}