Use this code below to creat a code to create a function that accepts 2 strings:
ID: 3605854 • Letter: U
Question
Use this code below to creat a code to create a function that accepts 2 strings: the first is a string of text to process;
the second is where the output will be placed. rem1() should remove THE FIRST
occurrence of a lowercase 'x' from the first string, and save the resulting
string in the second arg.
#include<stdio.h>
#include<string.h>
int rem1(char*, char*);
int main(void)
{
char input[120];
char output[120];
scanf("%s", input); //stores input
rem1(input, output); //call to rem
printf("output: %s ", output); //prints output
return 0;
}
int rem1(char*input, char*out)
{
int a=strlen(input); //defines and stores strlen.
int index= 0; //variable for for statement
for(int i=0; i<a; i++)
{
if(input[i] != 'x')
{ out[index] = input[i];
index++;
}
}
return 0;
}
Explanation / Answer
#include<stdio.h>
#include<string.h>
int rem1(char*, char*);
int main(void)
{
char input[120];
char output[120];
scanf("%s", input); //stores input
rem1(input, output); //call to rem
printf("output: %s ", output); //prints output
return 0;
}
int rem1(char*input, char*out)
{
int a=strlen(input); //defines and stores strlen.
int index= 0, count = 0; //variable for for statement
for(int i=0; i<a; i++)
{
if(input[i] != 'x' || count != 0)
{
out[index] = input[i];
index++;
}
if(input[i] == 'x')
count++;
}
out[index] = '';
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.