During the interview, the interviewer asked me to write an algorithm title: Given a string and a character, the deletion string contains the same character as this character.
Algorithm 1:
1#include <stdio.h>2 3 voidDelchar (Char* STR,Charp) {4 inti =0;5 intj =0;6 while(Str[j]! =NULL) {7 if(Str[j] = =p)8J + +;9 Else{TenStr[i] =Str[j]; Onei++; AJ + +; - } - } theStr[i] =' /'; - } - - intMain () { + Chartest[ -] ="www.baidu.com"; -Delchar (Test,'W'); + puts (test); A //Free (s); at return 0; -}
Algorithm 2: (Dynamic allocation of space)
1#include <stdio.h>2#include <string.h>3#include <malloc.h>4 5 Char* Delchar (Char* STR,Charx) {6 intLen =strlen (str);7 if(str = =NULL)8 returnNULL;9 Ten Char*p = (Char*)malloc(Len +1); One Char*q =p; A while(*str! =' /'){ - if(*str = =x) -str++; the Else { -*p++ = *str; -str++; - } + } -*p =' /'; + returnQ; A } at - intMain () { - Chartest[ -] ="www.baidu.com"; - Char*s = Delchar (test,'W'); - puts (s); - Free(s); in return 0; -}
Algorithm to delete a given character