C program to replace the first occurrence of a character in the entered string using a while a loop. | C प्रोग्राम एक लूप का उपयोग करते हुए दर्ज स्ट्रिंग में एक चरित्र की पहली घटना को बदलने के लिए।












C program to replace the first occurrence of a character in the entered string using a while a loop.




C program to replace the first occurrence of a character in the entered string using a while a loop.



#include <stdio.h> #define MAX 100 void replaceF(char * text, char oldC, char newC); int main() { char text[MAX], oldC, newC; printf("\n Enter any string: "); gets(text); printf("\n Enter character to replace in the given string: "); oldC = getchar(); getchar(); printf("Enter character to replace in the given string '%c' with: ", oldC); newC = getchar(); printf("\n String before replacing: %s", text); replaceF(text, oldC, newC); printf("\n String after replacing the first occurring character '%c' with '%c' : %s", oldC, newC, text); return 0; } void replaceF(char * text, char oldC, char newC) { int i=0; while(text[i] != '\0') { if(text[i] == oldC) { text[i] = newC; break; } i++; } }
Output:
Enter any string: G Rama Enter character to replace: G Enter character to replace 'G' with C String before replacing: G Rama String after replacing first 'G' with 'C': C Rama


Exercise: 1
C program to replace first occurrence of a character in a string.