रनटाइम पर एक स्ट्रिंग दर्ज करें और कंसोल पर बदलने के लिए एक कैरेक्टर पढ़ें। फिर, अंत में एक नया कैरेक्टर पढ़ें जिसे पुराने कैरेक्टर के स्थान पर रखा जाना है जहां भी वह स्ट्रिंग के अंदर होता है।
प्रोग्राम 1
कैरेक्टर की सभी घटनाओं को बदलने के लिए C प्रोग्राम निम्नलिखित है:
#include <stdio.h>
#include <string.h>
int main(){
char string[100], ch1, ch2;
int i;
printf("enter a string : ");
gets(string);
printf("enter a character to search : ");
scanf("%c", &ch1);
getchar();
printf("enter a char to replace in place of old : ");
scanf("%c", &ch2);
for(i = 0; i <= strlen(string); i++){
if(string[i] == ch1){
string[i] = ch2;
}
}
printf("\n the string after replace of '%c' with '%c' = %s ", ch1, ch2, string);
return 0;
}
आउटपुट
जब उपरोक्त प्रोग्राम चलाया जाता है, तो यह निम्नलिखित परिणाम देता है:
enter a string: Tutorials Point enter a character to search: i enter a char to replace in place of old: % the string after replace of 'i' with '%' = Tutor%als Po%nt enter a string: c programming enter a character to search: m enter a char to replace in place of old: $ the string after replace of 'm' with '$' = c progra$$ing
प्रोग्राम 2
पहली घटना पर बदलने के लिए C प्रोग्राम निम्नलिखित है:
#include <stdio.h>
#include <string.h>
int main(){
char string[100], ch1, ch2;
int i;
printf("enter a string : ");
gets(string);
printf("enter a character to search : ");
scanf("%c", &ch1);
getchar();
printf("enter a char to replace in place of old : ");
scanf("%c", &ch2);
for(i = 0; string[i] != '\0'; i++){
if(string[i] == ch1){
string[i] = ch2;
break;
}
}
printf("\n the string after replace of '%c' with '%c' = %s ", ch1, ch2, string);
return 0;
}
आउटपुट
जब उपरोक्त प्रोग्राम चलाया जाता है, तो यह निम्नलिखित परिणाम देता है:
Run 1: enter a string: Tutorial Point enter a character to search: o enter a char to replace in place of old: # the string after replace of 'o' with '#' = Tut#rial Point Run 2: enter a string: c programming enter a character to search: g enter a char to replace in place of old: @ the string after replace of 'g' with '@' = c pro@ramming