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

I need help with writing the following string functions in C please provide expl

ID: 3762262 • Letter: I

Question

I need help with writing the following string functions in C please provide explanations so I can understand how it works. Thank you very much!

(1) void removeChar(char *str, char c) -

this function should modify the input string, str to remove all instances of the character in the variable c and shift any subsequent characters forward. For example, a call to this function on the string “Hello World”, ‘o’ would result in the string “Hell Wrld”.

(2) void replaceChar(char *src, char oldChar, char newChar) -

this function replaces instances of a given character with a different character in a string. It will replace any instance of the character stored in oldChar with the character stored in newChar in the string src.

(3) int strContains(const char *str, const char *subStr) -

this function determines if a given string str contains a substring subStr. The function should return true if subStr appears anywhere inside str, false otherwise.

Explanation / Answer

delete char from string:
#include<stdio.h>
#include<conio.h>
#include<string.h>

void del(char str[], char ch);//declared global function string,char

void main() {
char str[10];//string length
char ch;//for delete charcter

printf(" Enter the string : ");//to read input string
gets(str);

printf(" Enter character which you want to delete : ");//to read delete char from string
scanf("%ch", &ch);

del(str, ch);//delete char
getch();
}

void del(char str[], char ch)//called function
{
int i, j = 0;//intailization varieabls
int size;
char ch1;
char str1[10];

size = strlen(str);//process for cheking the delete char is

for (i = 0; i < size; i++) // not find no element is deleted
{
if (str[i] != ch) //if find element will be deleted
{   
ch1 = str[i];
str1[j] = ch1;
j++;
}
}
str1[j] = '';

printf(" corrected string is : %s", str1);
}

REPLACE A CHARECTER:
#include <stdio.h>
#include <string.h>

char *replace_str(char *str, char *orig, char *rep)//global function with three arguments
{
static char buffer[4096];//one for string, char for orignal element,char rep for replace char
char *p;

if(!(p = strstr(str, orig)))//if char is not find to replace it will be return

return str;//if find replace the char in particualr char place

strncpy(buffer, str, p-str);//serch the element
buffer[p-str] = '';

sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));

return buffer;//return the buffer
}

int main(void)
{
char str[100],str1[50],str2[50];//function called
printf("Enter a one line string.. ");//read input string
gets(str);
printf("Enter the sub string to be replaced.. ");//read replaced string
gets(str1);
printf("Enter the replacing string.... ");
gets(str2);
puts(replace_str(str, str1, str2));

return 0;


}

SUB STRING:
#include<stdio.h>

void main()
{
char str[80], search[10];//declared variebles with string size
int count1 = 0, count2 = 0, i, j, flag;//a flag is a predefined bit or bit sequence that holds a //binary value.

printf("Enter a string:");//read input string
gets(str);
printf("Enter search substring:");//read substring for search
gets(search);
while (str[count1] != '')//string count is not eaqual null then start the process
count1++;
while (search[count2] != '')//same process for substring
count2++;
for (i = 0; i <= count1 - count2; i++)//check the condition
{
for (j = i; j < i + count2; j++)
{
flag = 1;
if (str[j] != search[j - i])
{
flag = 0;
break;
}
}
if (flag == 1)
break;
}
if (flag == 1)
printf("SEARCH SUCCESSFUL!");//checking perocess over display the message
else
printf("SEARCH UNSUCCESSFUL!");
}