基础代码|字符串操作

1.反转字符串:

define NUM 80 #include int main(){ char cc[NUM],c='a'; int i=0,ncount=0; printf("please input a line char:\n"); gets(cc); printf("\n\nyou input:%s\n",cc); for(i=0; cc[i]!='\0'; i++){ ncount++; } printf("the length of %s is %d\n",cc,ncount); for(i=0; i

2.输入字符串的多种方式:
#define NUM 80 #include int main(){ char cc[NUM],bb[NUM],c; int i=0,ncount=0; printf("please input a line char: \n"); scanf("%s",cc); printf("\n\nyou input :%s\n",cc); for(i=0; cc[i]!='\0'; i++){ if(cc[i]>='A'&&cc[i]<'A'+25){ cc[i]=cc[i]+32; } } printf("after to lower Case:%s\n",cc); //方法一遇到空格就结束输入 //方法二 printf("\nplease input another string:\n "); gets(bb); printf("\nanother:\n%s\n",bb); for(i=0; bb[i]!='\0'; i++){ if(bb[i]>='A'&&bb[i]<'A'+25){ bb[i]=bb[i]+32; } } printf("\nafter to lower case:\n%s\n",bb); }

基础代码|字符串操作
文章图片




方法三
#define NUM 80 #include #include int main(){ char cc[NUM],c; int i=0,ncount=0; while(ncount='a'&&cc[i]<97+25){ cc[i]=cc[i]-32; } } printf("after tocUpper Case:%s\n",cc); }

基础代码|字符串操作
文章图片

3.在字符串中查询某个字符:
#define NUM 80 #include int main(){ char cc[NUM],c='a'; int i=0,nCount=0,nPos[NUM]; printf("please input a line char:\n"); gets(cc); printf("please input a char:\n"); c=getchar(); printf("you input:%s\n",cc); printf("you will find :%c\n",c); while(i



4.找到并删除字符
#define NUM 80 #include int main(){ char cc[NUM],c='a'; int i=0,j=0; printf("please input a line char:\n"); gets(cc); printf("please input a char:"); c=getchar(); printf("\nyou input:%s\n",cc); printf("you will find :%c\n",c); while(i

基础代码|字符串操作
文章图片

#define NUM 80 #include int main(){ char cc[NUM],c='a'; int i=0,j=0,nCount=0,nPos[NUM]; printf("please input a line char:\n"); gets(cc); printf("please input a char:"); c=getchar(); printf("\nyou input :%s\n",cc); printf("you will find :%c\n",c); while(i

基础代码|字符串操作
文章图片

5.找到字符串出现的位置
(1)找到一个字符串首次出现的位置
#define NUM 256 #include int main(){ char cc[NUM],findC[NUM]; int i=0,nPos=0,j=0,lensub=0; printf("please input a line char:\n"); gets(cc); printf("please input a find string:"); gets(findC); printf("\nyou input:%s\n",cc); printf("you will find :%s \n",findC); for(i=0; (findC[i]!='\0'&& i0){ break; } i++; } if(nPos>0){ printf("the position:%d\n",nPos); } else{ printf("not found!"); } }


基础代码|字符串操作
文章图片

【基础代码|字符串操作】(2)找到一个字符串各次出现的位置
#define NUM 256 #include int main(){ char cc[NUM],findC[NUM]; int i=0,nPostmp=0,j=0,lensub=0,nCount=0,nPos[NUM]; printf("please input a line char:\n"); gets(cc); printf("please input a find string:"); gets(findC); printf("\nyou input :%s\n",cc); printf("you will find :%s\n",findC); for(i=0; (findC[i]!='\0'&&i

基础代码|字符串操作
文章图片

6.删除一个子串各次出现
(1)使用复制到新数组的方法:
#define NUM 256 #include int main(){ char cc[NUM],findC[NUM],newcc[NUM]; int i=0,nPos[NUM],nCount=0,j=0,lensub=0,nPostmp=0,k=0; printf("please input a line char:\n"); gets(cc); printf("please input a find string:"); gets(findC); printf("\nyou input\n"); printf("%s\n",cc); printf("you will find:%s \n",findC); for(i=0; (findC[i]!='\0'&& i

基础代码|字符串操作
文章图片

(2)直接在原数组中移动以删除子串:
#include #define NUM 256 int main(){ char cc[NUM],newcc[NUM],findC[NUM]; int i=0,nPos[NUM],nCount=0,j=0,lensub=0,nPostmp=0,k=0; printf("please input a line char:\n"); gets(cc); printf("please input a find string:"); gets(findC); printf("you will find:%s\n",findC); for(i=0; i

基础代码|字符串操作
文章图片




7.在字符串的指定位置插入字符串
#define NUM 256 #include int main(){ char cc[NUM],findC[NUM]; int i=0,j=0,insertIndex=0,ncc=0,nfindc=0; printf("please input the line char:"); gets(cc); printf("please input the insert string:"); gets(findC); printf("%s\n",cc); scanf("%d",&insertIndex); for(i=0; (findC[i]!='\0'); i++); nfindc=i; for(i=0; (cc[i]!='\0'); i++) ncc=i; ncc++; for(i=ncc; i>=insertIndex; i--){ cc[i+nfindc]=cc[i]; } for(i=0; i

基础代码|字符串操作
文章图片

8.获取字符串的一部分
#define NUM 256 #include int main(){ char cc[NUM],findC[NUM]; int i=0,j=0,iStart=0,iEnd=0; printf("please input a line char:\n"); gets(cc); printf("%s\n",cc); printf("please input start position,end position:"); scanf("%d %d",&iStart,&iEnd); for(i=iStart; (i

基础代码|字符串操作
文章图片

9.将两个字符串连接起来
#define NUM 256 #include int main(){ char cc[NUM],cc2[NUM]; int i=0,j=0,lencc=0,lencc2=0; printf("please input a first string:\n"); gets(cc); printf("please input a secondary string:\n"); gets(cc2); for(i=0; cc[i]!='\0'; i++){ lencc=i; } lencc++; for(i=0; cc2[i]!='\0'; i++){ lencc2=i; } lencc2++; for(i=0; i

基础代码|字符串操作
文章图片

10.两个字符串进行比较


    推荐阅读