Replace the first occurrence of a character with another in a string with C.
#include <stdio.h>
/* Function declaration */
void replaceFirst(char *str, char oldChar, char newChar);
int main()
{
char str[100], oldChar, newChar;
printf("Enter any string: ");
gets(str);
printf("Enter character to replace: ");
oldChar = getchar();
// Used to skip extra ENTER character
getchar();
printf("Enter character to replace '%c' with: ", oldChar);
newChar = getchar();
printf("\nString before replacing: %s\n", str);
replaceFirst(str, oldChar, newChar);
printf("String after replacing first '%c' with '%c' : %s", oldChar, newChar, str);
return 0;
}
/**
* Replace first occurrence of a character with
* another in given string.
*/
void replaceFirst(char *str, char oldChar, char newChar)
{
int i = 0;
/* Run till end of string */
while (str[i] != '\0')
{
/* If an occurrence of character is found */
if (str[i] == oldChar)
{
str[i] = newChar;
break;
}
i++;
}
}
Output
Enter any string : Learn With Reza
String before removing duplicates : Learn With Reza
String after removing duplicates : Learn WithRz
0 Comments