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

To remove first occurrence of a character in a string using for loop, while loop and functions in c programs:

  • 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

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *