Could you please complete with steps and explanations. thank you. #include<stdio
ID: 674215 • Letter: C
Question
Could you please complete with steps and explanations. thank you.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char fname[20];
printf("Enter your first name again: ");
scanf("%s", fname);
int len = strlen(fname); // length of first name
// declare another string of same length (dynamic declaration)
char *str = (char *) malloc(sizeof(char) * len + 1);
// initialize the string with '_'s
initializeBlankString(str, len);
char c;
printf("Enter a letter: ");
getchar();
scanf("%c", &c);
// check if the letter is present in your first name
int check = checkLetter(fname, c);
// If present, put that letter in the exact same position in the 2nd string (filled with '_'s)
int flag = revealGuessedLetter(c, str, fname);
// Output if the second string has been updated and print both the strings
return 0;
}
/* Complete the functions */
revealGuessedLetter( , , ){
}
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void initializeBlankString(char [], int);
int checkLetter(char *, char);
int revealGuessedLetter(char, char *, char *);
int main(){
char fname[20];
printf("Enter your first name again: ");
scanf("%s", fname);
int len = strlen(fname); // length of first name
// declare another string of same length (dynamic declaration)
char *str = (char *) malloc(sizeof(char) * len + 1);
// initialize the string with '_'s
initializeBlankString(str, len);
char c;
printf("Enter a letter: ");
getchar();
scanf("%c", &c);
// check if the letter is present in your first name
int check = checkLetter(fname, c);
// If present, put that letter in the exact same position in the 2nd string (filled with '_'s)
if (check == 1) {
int flag = revealGuessedLetter(c, str, fname);
if(flag == 1) {
printf("Entered string %s ", fname);
printf("Modified string %s ", str);
}
}
// Output if the second string has been updated and print both the strings
return 0;
}
/* Complete the functions */
void initializeBlankString(char str[], int len) {
int i;
for (i=0; i<len; i++) {
str[i] = '_';
}
str[i]='';
}
int checkLetter(char *fname, char letter){
int i=0;
while (fname[i] != '') { //looping till end of fname
if(fname[i] == letter)
return 1; //if found letter in fname, return 1
i++; //increasing index
}
//else it will default return 0 as not found
return 0;
}
int revealGuessedLetter(char c, char *str, char *fname) {
int i=0;
while (fname[i] != '') { //looping till end of fname
if(fname[i] == c)
str[i] = c;
i++;
}
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.