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

C language Your task for this activity is to implement the following four functi

ID: 3852708 • Letter: C

Question

C language

Your task for this activity is to implement the following four functions:

a. initializeBlankString():
This function should take two variables as input: an integer denoting the length of the second argument, which should be a character array. It should return nothing. The function should alter the passed array so that it is filled with ‘_’ and is a properly terminated string.

Given:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Function prototype should be placed below


int main(void){
char s[25];
int n = 10; // size of the string

// Intialize the string with '_'s
initializeBlankString( s, n);

// print the string here

return 0;
}

/* Complete the function using proper return type and arguments */

initializeBlankString( , ){

}

b. printWithSpaces():

This function will take a string as input and print the contents of the string with spaces between each character. (Hint: use the strlen() function to find the size of the passed string). The function should return nothing.

Given:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Function prototype should be placed below

int main(void){
char fname[20];
printf("Enter your first name: ");
scanf("%s", &fname);
  
// strlen() function used to calculate the length of the string
int len = strlen(fname);
printf("There are %d letters in your first name ", len);

// TODO: print each letter in a separate line

//TODO: print all letters in a single line but put a space between each letters
printWithSpaces(fname);
return 0;
}

/* Complete the following function */
/* Use proper return type and arguments */
printWithSpaces(){

}

c. checkLetter() :
The function will take a character and string as input. It checks if the letter is present inside the string. If Yes, it returns 1, else it returns 0.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Function prototype should be placed below


int main(void){
  
char fname[20];
printf("Enter your first name: ");
scanf("%s", fname);

//check if a letter present in your firstname

char letter;
printf("Enter a letter: ");
getchar();
scanf("%c", &letter);
int check = checkLetter(fname, letter);

//TODO: output if the letter present or not

return 0;
}

/* Complete the function */

checkLetter( , ){

}

Explanation / Answer

Code for part A:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Function prototype should be placed below
void initializeBlankString( char s[], int n );

int main(void){
   char s[25];
   int n = 10; // size of the string

   // Intialize the string with '_'s
   initializeBlankString( s, n);

   // print the string here
   printf("%s", s);
   return 0;
}

/* Complete the function using proper return type and arguments */

void initializeBlankString( char s[], int n )
{
   int i;
   for(i; i<n; i++)
       s[i] = '_';
   s[i] = '';         //To mark end of string
}

Code for Part B:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Function prototype should be placed below
void printWithSpaces(char fname[]);

int main(void)
{
    char fname[20];
    printf("Enter your first name: ");
    scanf("%s", &fname);
  
    // strlen() function used to calculate the length of the string
    int len = strlen(fname);
    printf("There are %d letters in your first name ", len);

    // TODO: print each letter in a separate line
   for(int i=0; i<len; i++)
       printf("%c ", fname[i]);

    //TODO: print all letters in a single line but put a space between each letters
    printWithSpaces(fname);
    return 0;
}

/* Complete the following function */
/* Use proper return type and arguments */
void printWithSpaces(char fname[])
{
   int len = strlen(fname);
   for(int i=0; i<len; i++)
       printf("%c ", fname[i]);
   printf(" ");
}

Code for Part C:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Function prototype should be placed below
int checkLetter (char fname[], char letter);

int main(void){

   char fname[20];
   printf("Enter your first name: ");
   scanf("%s", fname);
   
   //check if a letter present in your firstname

   char letter;
   printf("Enter a letter: ");
   getchar();
   scanf("%c", &letter);
   int check = checkLetter(fname, letter);

   //TODO: output if the letter present or not
   if(check==1)
        printf("Letter %c is present in the first name.", letter);
   else
        printf("Letter %c is not present in the first name.", letter);

   return 0;
}

/* Complete the function */

int checkLetter (char fname[], char letter)
{
   for(int i=0; i<strlen(fname); i++)
   {
       if(fname[i]==letter)           //Letter found
           return 1;
   }
   return 0;               //Letter not found over the whole word.
}

The above 3 codes should be self-explanatory. However, should any doubt still persist, feel free to comment below and it will be solved at the earliest.