字符数组
#include <iostream> using namespace std; void zifushuzu1() { //字符数组初始化 char a[3][3]={{'a','b','c'},{'d','e'},{'f','g','h'}}; for (int i=0;i<3;i++) { for (int j=0;j<3;j++) { cout<<a[i][j]<<" "; } cout<<endl; } } void zifushuzu2() { //gets puts char str[20]; gets(str); //"\0":表示字符串结束 puts(str); } #include<cstring> void zifushuzu3() { //cin.getline() char a[40]; cin.getline(a,39); cout<<a<<endl; cin.get(a,39,'e');//'e' 为结束字符 cout<<a<<endl; int n=strlen(a);//返回字符串长度 cout<<n<<endl; }
求逆序字符reverse

void zifushuzu4() { //求逆序字符reverse const int N=100; char str1[N],str2[N]; cin.getline(str1,N-1); int len=strlen(str1); for(int i=len-1; i>=0; i--) str2[len-i-1]=str1[i]; str2[len]='\0'; puts(str2); }
求数的和


void zifushuzu5() { //求数的和 char N[205]; int sum=0; //scanf("%s",N);//N指向数组首地址,无需再加取地址符& cin.getline(N,204); int Len=strlen(N); for(int i=0; i<Len; ++i) sum+=N[i]-'0'; //printf("%d\n",sum); cout<<sum; }
查找子串

void zifuchuan6() { //查找子串 int N=100 ; int i,j,k; char s1[N],s2[N]; cin.getline(s1,N-1); cin.getline(s2,N-1); for(i=0; s1[i]!='\0'; i++) { //for(j=i,k=0; s1[j]!='\0' && s2[k]!='\0' && s1[j]==s2[k]; j++,k++); for (j=i,k=0;;j++,k++) { if (s1[j]!='\0' && s2[k]!='\0' && s1[j]==s2[k]) { continue; } else { break; } } if(s2[k]=='\0') //若s2比较完毕,表示它是s1的子串 { cout<<i<<endl; return ; } } cout<<"-1\n"; }
字符串游戏

void zifuchuan7() { //字符串游戏 char a[60],b[60]; cin.getline(a,59); cin.getline(b,59); int alen=strlen(a); int blen=strlen(b); for(int j=0; j<blen-1; j++)//对b[]冒泡排序 for(int i=0; i<blen-j-1; i++) if(b[i]<b[i+1]) swap(b[i],b[i+1]); for(int i=0,j=0; i<alen && j<blen; i++)//遍历一遍a[] if(b[j]>a[i]) //如果可以覆盖,则覆盖 { a[i]=b[j]; j++; } puts(a); }
柱状图

void zifuchuan8() { //柱状图 //freopen("chart.in","r",stdin); //freopen("chart.out","w",stdout); int MAX=0,b[26]= {0}; //b数组相当于桶排序里的桶 for(int i=0; i<4; i++) //读取4行字符串 { char a[101]; cin.getline(a,100); for(int j=0; j<strlen(a); j++) if(a[j]>='A' && a[j]<='Z') b[a[j]-'A']++; } for(int i=0; i<26; i++) //MAX用于统计出现最多的字母 MAX=max(MAX,b[i]); for(int i=MAX; i>0; i--) //输出柱状图 { for(int j=0; j<26; j++) { //printf("%c%c",b[j]>=i?'*':' ',j==25?'\n':' '); if (b[j]>=i) { cout<<'*'; } else { cout<<' '; } if (j==25) { cout<<endl; } else { cout<<' '; } } } for(char x='A'; x<='Z'; x++) { //printf("%c%c",x,x=='Z'?'\n':' '); cout<<x; if (x=='Z') { cout<<endl; } else { cout<<' '; } } }


strcmp 比较2个字符串
void zifuchuan9() { //strcmp 比较2个字符串 //str1==str2 返回0; str1<str2 返回负整数; str1>str2 回正整数; char str1[]="HELLO!"; char str2[]="HELLO1!"; cout<<strcmp(str1,str2); }
 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号