欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

字符串操作

程序员文章站 2022-03-08 14:05:21
...

1.反转字符串:

define NUM 80
#include <stdio.h> 
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<ncount/2;i++){
		c=cc[i];
		cc[i]=cc[ncount-1-i];
		cc[ncount-1-i]=c;
	}
	printf("after convert:\n");
	printf("%s\n",cc);
}

2.输入字符串的多种方式:

#define NUM 80
#include <stdio.h>
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 <stdio.h>
#include <conio.h>
int main(){
	char cc[NUM],c;
	int i=0,ncount=0;
	while(ncount<NUM){
		c=getch();
		putchar(c);
		
		//回车符,跳转到当前行头部 
		if(c=='\r'){
			break;
		} 
		cc[ncount]=c;
		ncount++; 
	}
	cc[ncount]='\0';
	printf("\n\nyou input:%s\n",cc);
	for(i=0;cc[i]!='\0';i++){
		if(cc[i]>='a'&&cc[i]<97+25){
			cc[i]=cc[i]-32;
		}
	}
	printf("after tocUpper Case:%s\n",cc);
}

字符串操作

3.在字符串中查询某个字符:

#define NUM 80
#include <stdio.h>
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<NUM && cc[i]!='\0'){
		if(cc[i]==c){
			nPos[nCount]=i;
			nCount++;
		}
		i++;
	}
	printf("the number of %c in %s is:%d\n",c,cc,nCount);
	for(i=0;i<nCount;i++){
		printf("%4d",nPos[i]);
	}
	printf("\n");
}


4.找到并删除字符

#define NUM 80
#include <stdio.h>
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<NUM && cc[i]!='\0'){
		if(cc[i]==c){
			for(j=i;cc[j]!='\0';j++){
				cc[j]=cc[j+1];
			}
			i--;
		}
		i++;
	}
	printf("\n%s\n",cc);
}

字符串操作

#define NUM 80
#include <stdio.h>
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<NUM && cc[i]!='\0'){
		if(cc[i]==c){
			nPos[nCount]=i;
			nCount++;
		}
		i++;
	}
	for(i=0;i<nCount;i++){
		printf("%4d",nPos[i]);
	}
	for(j=0;j<nCount;j++){
              //最后一次出现
              if(j==nCount-1){
			for(i=nPos[j]+1;cc[i]!='\0';i++){
				cc[i-j-1]=cc[i];
			}
			cc[i-j-1]='\0';
		}
		else{
                //前几次出现
	        for(i=nPos[j]+1;(i<nPos[j+1]&&cc[i]!='\0');i++){
	        	cc[i-j-1]=cc[i];
			}		
		}
		printf("\n%s\n",cc);
    } 
    printf("\n%s\n",cc);
}

字符串操作

5.找到字符串出现的位置

(1)找到一个字符串首次出现的位置

#define NUM 256
#include <stdio.h>
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'&& i<NUM);i++);
	lensub=i;
	printf("the length of sub:%d\n",lensub);
	i=0;
	while(i+lensub<NUM && cc[i+lensub]!='\0'){
		nPos=i;
		for(j=0;j<lensub;j++){
			if(cc[i+j]!=findC[j])
			{
				nPos=-1;
				break;
			}
		}
		if(nPos>0){
			break;
		}
		i++;
	}
	if(nPos>0){
		printf("the position:%d\n",nPos);
	}
	else{
		printf("not found!");
	}
}

字符串操作

(2)找到一个字符串各次出现的位置

#define NUM 256  
#include <stdio.h>  
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<NUM);i++){  
        lensub=i;  
    } 
	lensub++; 
    printf("the length of sub:%d\n",lensub);  
    i=0;  
    while(i+lensub<NUM && cc[i+lensub-1]!='\0'){  
        nPostmp=i;  
        for(j=0;j<lensub;j++){  
            if(cc[i+j]!=findC[j]){  
                nPostmp=-1;  
                break;  
            }  
        }  
        if(nPostmp!=-1){  
            nPos[nCount]=nPostmp;
			nCount++;
			i=i+lensub;  
        }  
        else{
            i++;  
        }
	}  
 	
	printf("Pos:");
	for(i=0;i<nCount;i++)
	{
		printf("%d ",nPos[i]);
	}
} 
字符串操作

6.删除一个子串各次出现

(1)使用复制到新数组的方法:

#define NUM 256
#include <stdio.h>
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<NUM);i++){
		lensub=i;
	}
	lensub++;
	printf("the length of sub:%d\n",lensub);
	
	i=0;
	while(i+lensub<NUM && cc[i+lensub-1]!='\0'){
		nPostmp=i;
		for(j=0;j<lensub;j++){
	       if(cc[i+j]!=findC[j]){
	       	nPostmp=-1;
	       	break;
		   }		
		}
		if(nPostmp!=-1){
			nPos[nCount]=nPostmp;
			nCount++;
			i=i+lensub;
		}
		else{
			i++;
		}
	}
	for(i=0;i<nCount;i++){
		printf("%4d",nPos[i]);
	}
	printf("\n");
	
	k=0;
	for(j=0;j<nPos[0];j++){
		newcc[k]=cc[j];
		k++;
	}
	
	for(i=1;i<nCount;i++){
		for(j=nPos[i-1]+lensub;j<nPos[i];j++){
			newcc[k]=cc[j];
			k++;
		}
	}
	for(j=nPos[nCount-1]+lensub;cc[j]!='\0';j++){
		newcc[k]=cc[j];
		k++;
	}
	newcc[k]='\0';
	printf("after delete:\n%s\n",newcc);
} 
字符串操作

(2)直接在原数组中移动以删除子串:

#include <stdio.h>
#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<NUM&&findC[i]!='\0';i++)
	{
		lensub=i;
	}
	lensub++; 
	
	printf("the length of sub is: %d\n",lensub);
	
	i=0;
	while(i+lensub<NUM && cc[i+lensub-1]!='\0')
	{
		nPostmp=i;
		for(j=0;j<lensub;j++)
		{
			if(cc[j+i]!=findC[j])
			{
				nPostmp=-1;
				break;
			}
		}
		
		if(nPostmp!=-1)
		{
			nPos[nCount]=nPostmp;
			nCount++;
			i=i+lensub;
		}
		else
		{
			i++;
		}
	}
	
	printf("Pos:");
	for(i=0;i<nCount;i++)
	{
		printf("%d ",nPos[i]);
	}
	printf("\n");
	
	for(i=0;i<nCount-1;i++)
	{
		for(j=nPos[i]+lensub;j<nPos[i+1];j++)
		{
			cc[j-lensub*(i+1)]=cc[j];
		}
	}
	
	for(i=nPos[nCount-1]+lensub;cc[i]!='\0';i++)
	{
		cc[i-nCount*lensub]=cc[i];
	}
	cc[i-nCount*lensub]='\0';
	printf("after delete:\n%s\n",cc);
	
}

字符串操作


7.在字符串的指定位置插入字符串

#define NUM 256
#include<stdio.h>
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<nfindc;i++){
		cc[i+insertIndex]=findC[i];
	}
	printf("%s\n",cc);
}

字符串操作

8.获取字符串的一部分

#define NUM 256
#include<stdio.h>
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<iEnd&&cc[i]!='\0');i++){
    	findC[i-iStart]=cc[i];
	}
	findC[i-iStart]='\0';
	printf("%s",findC);
	printf("sub-string:%s\n",findC);
    
}

字符串操作

9.将两个字符串连接起来

#define NUM 256
#include<stdio.h>
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<lencc2;i++){
		cc[i+lencc]=cc2[i];
	}
	cc[i+lencc]='\0';
	printf("concat-string:\n%s\n",cc);
}

字符串操作

10.两个字符串进行比较


相关标签: 字符串 字符