12, a string of substrings related issues

12, delete the string substring-related issues

Delete a string of characters:

#include <stdio.h>
void del_char(char a[], char ch);
int main(){
	char a[6]={'D','D','d','i','d','s'}, ch='d';
	int i;

	del_char(a, ch); 
	for(i=0; i<6; i++){ 
		if(a[i] != 0){
			printf("%c", a[i]);
		}
	}
	printf("\n");
	return 0;
}
void del_char(char a[], char ch){
	int i;
	for(i=0;i<6;i++){
		if(a[i] == ch){
			a[i] = 0;	//若a[i]是指定要删除的字符ch,则将其置0(null)
		}
	}

Delete substring string:

#include <stdio.h>
#include <string.h>
void del_char(char a[], char ch[], int len);
int main(){
	char a[]="DDdids", ch[]="Dd";
	int len=strlen(ch);		//len记录子字符串的长度 
	//裁剪
	del_char(a, ch, len);
	
	printf("%s", a); 
	return 0;
}
void del_char(char a[], char ch[], int len){
	char *p, *q;		//p记录子串ch在''母串a''中第一次出现的起始地址 
	
	while(1){
		p=strstr(a,ch); 
		if(p==0){
			break;	//a中没有ch,则strstr函数返回空指针 
		}
		q=p+len;		//q记录'后一段'字符串的起始地址 
		*p='\0';		//此时a只有'前一段'字符串
		strcat(a,q);	//前后段字符串接一块 
	}
}

Delete all the letters in the string:

#include <stdio.h>
void delch(char a[],int n){
    int i,j;
    char s[10];

    for(i=0;i<n;i++){
        s[i]=a[i];	//将a存入s中
        a[i]='\0';	//同时清空a
    }

    for(i=0,j=0;s[i]!='\0';i++)
        if((s[i]>='0')&&(s[i]<='9')){
            a[j]=s[i];
            j++;
        }
}

void main(){
    char item[6]={'a','3','4','b','c','\0'};
    delch(item,6);
    printf("%s\n",item);
}

Delete the first character of the string:

p=&w[i][1];
strcpy(w[i],p);
Published 16 original articles · won praise 0 · Views 319

Guess you like

Origin blog.csdn.net/NAU_LHT/article/details/104333532