標籤:style blog color os io 資料 for ar
(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‘ ; }}