Algorithms used to delete specified characters in a C string

Source: Internet
Author: User

As shown in the following figure.

 


The question is quite obvious. Our thinking is actually quite simple. In other words, we just need to delete one and reconstruct the array, fill in the blank, and one character can be pushed one by one, you don't have to think too much about it (in simple words, it's just a lazy ).

The code is as follows: Copy code

# Include <stdio. h>
# Include <string. h>
Void delchar (char s [], char c );
Int main (void)
{
Char c;
Char s [80];
Printf ("Input a string :");
Gets (s );
Printf ("Input a char :");
Scanf ("% c", & c );
Delchar (s, c );
Printf ("After deleted, the string is: % s", s );
Return 0;
}
Void delchar (char s [], char c)
{
Int a, B, e;
E = strlen (s );
For (a = 0; a <e; a ++ ){
If (s [a] = c ){
For (B = a; B <e; B ++)
S [B] = s [B + 1];
A = a-1;
 }
 }
}

Algorithm 2

The code is as follows: Copy code

# Include <stdio. h>
Char fun (char str [20], char ch)
{Int I, j;
For (I = 0; str [I]! = '\ 0'; I ++)
If (str [I] = ch) {for (j = I; str [j]! = '\ 0'; j ++) str [j] = str [j + 1];}
}
Void main ()
{Char str [20], ch;
Printf ("enter a string :");
Gets (str );
Printf ("enter you want delete letter :");
Ch = getchar ();
Fun (str, ch );
Printf ("% s", str );

}

Algorithm 3

For example, you can delete the first position of a specified character in a string.

The code is as follows: Copy code
Void strdel (char * str, char ch)
{
Char * p = str;
While (* p)
    {
If (* p = ch)
Break;
    }
If (* p)
    {
While (* p)
        {
* P = * (p + 1 );
P ++;
        }
    }
}

I changed the program after my classmates asked me, so I don't have to worry too much about it. =

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.