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

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

C program to remove first occurrence of a character in a string; Through this tutorial, we will learn how to remove first occurrence of a character in a string using for loop, while loop and functions in c programs.

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

  • 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 output 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 output 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 output of the above c program; as follows:

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

Recommended C Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published.