C Program to Remove First Occurrence of a Character in a String

In this tutorial, i am going to show you how to remove first occurrence of a character in a string with the help of for loop, while loop and functions in c programs.

All C Programs to Remove First Occurrence of a Character in a String

  • C Program to Remove First Occurrence of a Character in a String using For Loop
  • C Program to Remove First Occurrence of a Character in a String using While Loop
  • C Program to Remove First Occurrence of a Character in a String using Function

C Program to Remove First Occurrence of a Character in a String using For Loop

#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;
}

The result of the above c program; as follows:

Enter  the string : welcome to c programming
Enter character: o
welcme to c programming

C Program to Remove First Occurrence of a Character in a String using While Loop

/* C Program to Remove First Occurrence of a Character in a String */
 
#include <stdio.h>
#include <string.h>
 
int main()
{
  	char str[100], ch;
  	int i, len;
 
  	printf("\n Please Enter any String :  ");
  	gets(str);
  	
  	printf("\n Please Enter the Character that you want to Remove :  ");
  	scanf("%c", &ch);
  	
	len = strlen(str);
	   	
  	for(i = 0; i < len && str[i] != ch; i++);
  	
  	while(i < len)
  	{
  		str[i] = str[i + 1];
		i++;  
	}
	printf("\n The Final String after Removing First occurrence of '%c' = %s ", ch, str);
	
  	return 0;
}

The result of the above c program; as follows:

Please Enter any String :  hello world
Please Enter the Character that you want to Remove :  o
The Final String after Removing First occurrence of 'o' = hell world 

C Program to Remove First Occurrence of a Character in a String using Function

#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;
 
     
     
}

The result of the above c program; as follows:

Enter  the string : c programming
Enter character: p
c rogramming

More C Programming Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.