implement simple functions using a string. Read a string from a user and stored
ID: 3544688 • Letter: I
Question
implement simple functions using a string. Read a string from a user and stored it in a 1D character array. Implement a function to calculate the length of a string. Also implement search and replace function where you look for a character (provided
by the user) in the string and if the character is present in the string then replace all the occurrence of the
character by the character '#'.
the sample oytput is:
Enter a string : asdasfsadfsa
Input string is asdasfsadfsa
Length of the string is 12
Enter a character to replace: d
String after the replacement operation is as#asfsa#fsa
Thanks!
Explanation / Answer
#include<stdio.h>
int length1(char str[]){
int i,count=0;
for(i=0;str[i]!='';i++){
count++;
}
return count;
}
void main(){
char *str;
char ch;
int count = 0,i;
printf(" Enter the String: ");
scanf("%s ",str);
printf(" Enter the char to replace: ");
scanf("%c",&ch);
printf("%c character is : ",ch);
for(i=0;str[i]!='';i++){
if(str[i]==ch){
str[i]='#';
}
}
printf(" The length of the string is %d.",length1(str));
printf(" The string is %s.",str);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.