Delete characters in the string (C language)

topic:

Programmed to delete the specified character string given. After the original request to remove the specified character string can not be left empty position, and specify the character string input by keyboard

The basic idea

The string is compared with the character to be deleted, if it is the same character, the string of the next character to character to replace the original string and turn back the characters in advance, so as to achieve the purpose of deleting characters. Note : The characters move forward one needs to judge whether or not to move to the current location of the character needs to continue to delete.

Algorithm Description

  1. Input from the keyboard and specified character string
  2. And with cycling string of characters followed by comparison, until the end of the string
  3. If not specified character string is not altered; if there is a specified character is the character after the subscript characters are sequentially advance a
  4. Save a loop control variable, it is determined again whether the character at that position specified character, repeat steps 2 and 3

Code

# include<stdio.h>
# include<string.h>
int main()
{
    int i, j, k;
    char a[1000];
    char b[1000];
    gets(a);//输入字符串 
    gets(b);//输入指定字符(可以是多个) 
    for(i=0;a[i]!='\0';i++)//遍历a数组 
    {
        for(j=0;b[j]!='\0';j++)//遍历b数组 
        {
            if(a[i]==b[j])//如果含有指定字符则开始替换后面字符 
            {
                for(k=i;a[k]!='\0';k++)//定义新的变量开始循环赋值 
                a[k]=a[k+i];
                i--;//下一轮循环会到新赋值的字符位置,继续比较 
            }
        }
    }
    printf("%s",a);
}

Analysis of Algorithms

Point of the algorithm is to complement and complement position after re-determination

Guess you like

Origin www.cnblogs.com/Xloading/p/12014185.html