C program to remove the first occurrence of a character from a string – In this article, we will brief in on the numerous methods to remove the first occurrence of a character from a string in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The methods used in this piece are as follows:
- Using Standard Method
- Using Function
- Using Recursion
A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.
As you can see in the example given above, firstly, you need to enter the specified string of your choice.
The string entered here is as follows:
“hello world”
Then, you need to particularise a character which you want to remove on its first occurrence.
The character ‘w’ is chosen for this string. Hence, the new string looks like this after ‘w’ is removed:
“hello orld”
Thus, the ways to do the same in C programming are as follows:
Using Standard Method
- Read the entered string and store the string into the character array variable ‘s’ using gets(s).
2) Read the entered character using getchar() and store the character in the variable c.
3) Iterate the first for loop till the end of the character of the string with the structure for(i=0;s[i];i++).
Then initialize the i value to n. Here i indicates the length of the string.
5) If the character is equal to the element of the string, then shift the elements of the string which are right to the removed element, to one position left.
6) Print the string after removing the first occurrence of the character from the string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include <stdio.h> #include <string.h> int main() { char s[1000],c,temp=1; int i,j,k,count=0,n; printf("Enter the string : "); gets(s); printf("Enter character: "); c=getchar(); for(i=0;s[i];i++); n=i; for(i=0;i<n;i++) { if(temp) { if(c==s[i]) { temp=0; s[i]=s[i+1]; } } else s[i]=s[i+1]; } printf("%s",s); return 0; } |
Output:
1 2 3 4 5 6 7 8 |
Enter the string: hello world Enter character: w hello orld Enter the string: alamala Enter character: m alaala |
Using Function
- The main() function calls the stringlength(char *s) by passing the string as an argument.
2) The stringlength(char *s) returns the length of the string.
3) Then function deletechar(char *s, char c) will remove the first occurrence of the character from the string.
temp=1.
For loop iterates through the string until the end of the string.
a) if temp=1
If the entered character is equal to the element of the string then
Initialize the temp=0 and shift the element right to the removed element to one position left.
b) If temp!=1 then shift the next element to the shifted element to one position left.
After all the iterations of the for loop, we will get the string with the characters except the character which we want to remove.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include <stdio.h> #include <string.h> int stringlength(char *s) { int j; for(j=0;s[j];j++); return j; } void deletechar(char *s,char c) { int i,temp=1,n; n=stringlength(s); for(i=0;i<n;i++) { if(temp) { if(c==s[i]) { temp=0; s[i]=s[i+1]; } } else s[i]=s[i+1]; } } int main() { char s[1000],c; printf("Enter the string : "); gets(s); printf("Enter character: "); c=getchar(); deletechar(s,c); printf("%s",s); return 0; } |
Output:
1 2 3 |
Enter the string: welcome to Cbeginners Enter character: C welcome to beginners |
Using Recursion
- The function stringlength(char *s) returns the length of the given string.
2) temp=1,i=0.
If i< the length of the string
a) if temp=1
If the entered character is equal to the element of the string then temp=0 and shift the element right to the removed character to one position left.
b) if temp!=1 then shift the next element to the shifted element to one position left.
Increase the i value by 1.
Call the function itself by taking the modified string, character to be removed as arguments. The function calls itself recursively until i<length of the string becomes false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#include <stdio.h> #include <string.h> int stringlength(char *s) { int j; for(j=0;s[j];j++); return j; } void deletechar(char *s,char c) { static int i,temp=1,n=stringlength(s); if(i<n) { if(temp) { if(c==s[i]) { temp=0; s[i]=s[i+1]; } } else s[i]=s[i+1]; i++; deletechar(s,c); } } int main() { char s[1000],c; printf("Enter the string : "); gets(s); printf("Enter character: "); c=getchar(); deletechar(s,c); printf("%s",s); return 0; } |
Output:
1 2 3 |
Enter the string: abcd abcd abcde abcd Enter character: e abcd abcd abcd abcd |