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. =