Comprehensively practice the basic knowledge of string, function and pointer
The key to this problem is the pointer, which can continuously process the source data.
The title comes from my university teacher. If there is infringement, you can contact the author to delete it. If there is a simpler algorithm, you are also welcome to add qq for discussion.
Author qq: 3293831515
Title: define a character array in the main function, initialize a string "ababcabcd" for the array, and define four sub functions fun1, fun2, fun3 and fun4 to find, replace, insert and delete the string respectively. Enter integers 1 ~ 4 in the main function to select what kind of processing to do. When an integer less than 1 or greater than 4 is entered, the program will end, otherwise it will continue to cycle. The running effect of the program is shown in the figure. Tips:
(1) This problem is still a problem of while(1) nested selection structure.
(2) After each option value in this question is entered, a character must be entered immediately. When the value is entered first and the character is entered later, the "carriage return" after the value will be considered as a valid character and stored in the variable. In order to store the real character in the variable, the character input sentence needs special treatment, which can be referred to as follows: scanf ("% C% C", & X, & Y); The first variable x has no practical significance. Its function is to receive the "carriage return" after the value. The real character is stored in the variable y.
(3) If there is a space between the two characters, as shown in the right figure, there is a space between the letter 'a' and the letter 'a', the input statement should be written as scanf ("% C% C,% C", & X, & Y, & z); Variable x is used to receive carriage return, variable y receives the letter 'a', variable Z receives the letter 'a', and there is a space between the following two%c.
(4) The method of "insert" character operation is: first move the characters behind the position to be inserted one cell in turn, and then place a new character at the position to be inserted.
(5) The method of "delete" character operation is to copy the characters in the original string to another character array, and pay attention to manually adding the terminator '\ 0' of the new string.
#include<stdio.h> #include<string.h> int fun1(char *p,char *a) { int sum=0; char x,y; printf("[[character search] Please enter a character to be searched:"); scanf("%c%c",&x,&y); for(p=a;p<(a+10);p++) { if(*p==y) { sum++; } } printf("[Character search] number of characters to be searched:%d",sum); } int fun2(char *a) { int i; char x,y,z; printf("[[character replacement] Please enter a character to be found and a replacement character:"); scanf("%c%c %c",&x,&y,&z); for(i=0;i<10;i++) { if(a[i]==y) { a[i]=z; } } printf("[Character replacement%c Replace letter%c The string after is:%s",z,y,a); } int fun3(char *a) { int n,i; char p,j,b[11]; printf("[[character insertion] Please enter a character and the position to be inserted:"); scanf("%c%c",&j,&p); scanf("%d",&n); for(i=0;a[i]!='\0';i++) { b[i]=a[i]; b[n]=p; } for(i=n;b[i]!='\0';i++) { b[i+1]=a[i]; } printf("%s",b); } int fun4(char *p,char *a) { char x,t,y,z; printf("[[character deletion] Please enter the character to be deleted:"); scanf("%c%c",&x,&y); printf("Delete character%c The new string after is:",y); for(p=a;p<(a+10);p++) { if(*p!=y) { printf("%c",*p); } } } int main() { int i; char a[50]={"ababcabcd"}; char *p=a; printf("**********************************************\n"); printf("\t [[string processing]\t\n"); printf(" String operation options:<1.Find 2.Replace 3.Insert 4.delete>\n"); printf(" The original array is: ababcabcd\n"); printf("**********************************************\n"); while(1) { printf("\n\n Please enter an option value;"); scanf("%d",&i); if(i==1) { fun1(p,p); } else if(i==2) { fun2(a); } else if(i==3) { fun3(a); } else if(i==4) { fun4(p,p); } else { printf("Invalid value, the program ends"); break; } } return 0; }