(1)我的错误程序:
#include<iostream> #include<string.h> using namespace std; int ZhuanHuan(char); int main() { char a[100]; cout<<"please input a string: "<<endl; cin>>a; for(int i=0;i<strlen(a);i++) { ZhuanHuan(a[i]); } for(int j=0;j<strlen(a);j++) { cout<<a[j]; } return 0; } int ZhuanHuan(char index) { int temp; if(index>='a'&&index<='z') { temp=(int)index; temp=index+32; index=(char)temp; }else if(index>='A'&&index<='Z') { temp=(int)index; index=index-32; index=(char)temp; }else { return 0; } }
(2 )修改后的程序:
#include<iostream> #include<string.h> using namespace std; void ZhuanHuan(char &); //不需要返回值 int main() { char a[100]; cout<<"please input a string: "<<endl; cin>>a; for(int i=0;i<strlen(a);i++) { ZhuanHuan(a[i]); } for(int j=0;j<strlen(a);j++) { cout<<a[j]; } return 0; } void ZhuanHuan(char &index) //1.不需要返回值,用void, 2.因为要带回数据,所以要用引用 { if(index>='a'&&index<='z') { index += 'A'-'a' ; }else if(index>='A'&&index<='Z') { index += 'a'-'A' ; } }