c++语言基础--数组.ppt
第五章 数组 例5.16 对给定的10个国家名,按其字母的顺序输出。 【参考程序1】 #include #include #include using namespace std; int main() { char t[21],cname[11][21]; for (int i=1; i<=10; ++i) gets(cname[i]); //gets为专门读字符串的函数, 读取一行字符串 for (int i=1; i<=9; ++i) { int k=i; for (int j=i+1; j<=10; ++j) if (strcmp(cname[k],cname[j])>0) k=j; strcpy(t,cname[i]); strcpy(cname[i],cname[k]); strcpy(cname[k],t); } for (int i=1; i<=10; ++i) cout< #include #include using namespace std; string cname[10]; int main(){ for (int i=0;i!=10;++i) getline(cin,cname[i]); sort(cname,cname+10);//利用C++库函数排序 for (int i=0;i!=10;++i) cout< #include #include #include using namespace std; string word,str; int main(){ int first=0,ans=0; cin>>word; word=" "+word+" "; for (string::size_type i=0;i!=word.size();++i)//string::size_type为无符类型 word[i]=toupper(word[i]); getline(cin,str); //当我们读完word后,还有换行符 getline(cin,str); str=" "+str+" "; for (string::size_type i=0;i!=str.size();++i) str[i]=toupper(str[i]); for (string::size_type i=str.find(word);i