Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need a C PROGRAM to implement simple functions using a string. Read a string f

ID: 3544829 • Letter: I

Question

I need a C PROGRAM to 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 '#'.

Function Prototypes:

int length(char a[]): This function takes a character array (string ) and return the length of the string. Use the null terminator ('') to locate the end of the string.

int replace(char [], char): As discussed above, this function takes a input string and a character to be searched in the string and if the character is present in the string then replace all the occurrence of the character by the character '#'.If the character is not present in the string it returns 0 otherwise 1.Use this returned value to print the appropriate message in the main.

main():Read a string from the user. You can read a string with %s notation (as shown in the code below) and store it directly in the character array. Similarly you can also print the entire string using the %s notation. Call the length and replace function and print the results as shown in the sample output below.


This is a skeleton for the main function:

#include<stdio.h>

#define MAX 100

int main()

{

       char x[MAX];

    

            printf(" Enter a string : ");

            scanf("%s", &x);

            

            printf(" Input string is %s ", x);

            

            int len=length(x);

            

            printf(" Length of the string is %d ", len);

            

            char c;

            scanf("%c", &c);

            

            printf(" Enter the a character to replace : ");

            scanf("%c", &c);

            

            int z =replace(x,c);

           ......

           ......

}


OUTPUT EXAMPLE:


./a.out


Enter a string : CS1050

Input string is CS1050

Length of the string is 6

Enter a character to replace: i

Character i is not present in the string.

./a.out

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


Explanation / Answer

#include<stdio.h>


#define MAX 100


int length(char *x)

{

return strlen(x);

}


int replace(char *x,char c )

{

int len = strlen(x);

int i,flag=0;

for(i=0;i<len;i++)

{

if(x[i]==c)

{

x[i]='#';

flag=1;

}

}

return flag;

}

int main()


{


char x[MAX];


printf(" Enter a string : ");


scanf("%s", &x);


printf(" Input string is %s ", x);


int len=length(x);


printf(" Length of the string is %d ", len);


char c;


scanf("%c", &c);


printf(" Enter the a character to replace : ");


scanf("%c", &c);


int z =replace(x,c);


if(z==1)

printf(" the string after replacement operation is %s ",x);

else

printf(" character %c is not present in the string ",c);


return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote