Question

Write C programs named mystring.h and mystring.c, containing the headers and implementations of the following functions....

Write C programs named mystring.h and mystring.c, containing the headers and implementations of the following functions.

  1. int letter_count(char *s) computes and returns the number of English letters in string s.
  2. int word_count(char *s) computes and returns the number of words in string s.
  3. void lower_case(char *s) changes upper case to lower case of string s.
  4. void trim(char *s) removes the unnecessary empty spaces of string s.

mystring.h

#include <stdio.h>
 
int letter_count(char *);
void lower_case(char *);
int word_count(char *);
void trim(char *);

mystring.c

#include "mystring.h"
 
int letter_count(char *s) 
{
// your implementation
}
 
void lower_case(char *s) {
// your implementation
}
 
int word_count(char *s) {
// your implementation
}
 
void trim(char *s) {
// your implementation
}

Use the provided main function to test the above functions.

#include "mystring.h"
 
int main(int argc, char* args[]) {
  setbuf(stdout, NULL);
  char str[100] = "     This Is    a Test   ";
  printf("\nInput string: \"%s\"", str);
  printf("\nLetter count:%d", letter_count(str));
  printf("\nWord count:%d", word_count(str));
  trim(str);
  printf("\nAfter trimming:\"%s\"", str);
  lower_case(str);  
  printf("\nAfter lower case:\"%s\"", str);
  return 0;
}

output should look like this:

Input string: "     This Is    a Test   "
Letter count:11
Word count:4
After trimming:"This Is a Test"
After lower case:"this is a test"
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the solution to above problem. I am using string.h and ctype.h for finding stringlength and checking if letter is an alphabet or converting to lowercase. Please refer to the code screenshot for indentation

HERE IS THE C CODE FOR PROGRAM

#include "mystring.h"
#include<stdio.h>
#include<string.h>
#include <ctype.h>

int letter_count(char *s)
{
   //finding string length
   int length= strlen(s);
   int i;
   int count=0;
  
   for(i=0;i<length;++i)
   { //checking if it is alphabet
       if(isalpha(s[i]))
           count++;   //incrementing count
   }
   return count;
}


void lower_case(char *s) {
   // stringlength using string.h library
   int length= strlen(s);
   int i;
   for(i=0;i<length;++i)
   {
       //using to toLower to convert to lower case
       s[i]=tolower(s[i]);  
   }
}

//counting the number of words
int word_count(char *s) {
// your implementation
   int length= strlen(s);
   int i;
   int count=0;
   for(i=0;i<length-1;++i)
   {
       //if counting for word if two consequtive space are not present
       if (s[i] == ' ' && s[i+1] != ' ')
       {
           count++;
       }
   }
   return count;

}


void trim(char *s) {
// your implementation
   int length= strlen(s);
   char * temp = (char *)malloc(sizeof(char)*length);
   int i=0;
   int j=0;
   int count=0;
   //removing prefix white spaces
   while(s[i]==' ')
   i++;
   //removing unnecessary spaces in between
   while(i<length-1)
   {
       if(!(s[i]==' '&& s[i+1]==' ') )
       {
           temp[j]=s[i];
           j++;
       }
       i++;
   }
   temp[j]='\0';
   //copying temp to original string
   for(i=0;i<strlen(temp);++i)
   {
       s[i]=temp[i];
   }
   s[i]='\0';
}


int main(int argc, char* args[]) {
setbuf(stdout, NULL);
char str[100] = " This Is a Test ";
printf("\nInput string: \"%s\"", str);
printf("\nLetter count:%d", letter_count(str));
printf("\nWord count:%d", word_count(str));
trim(str);
printf("\nAfter trimming:\"%s\"", str);
lower_case(str);
printf("\nAfter lower case:\"%s\"", str);
return 0;
}

OUTPUT OF CODE

SCREENSHOT OF CODE

Add a comment
Know the answer?
Add Answer to:
Write C programs named mystring.h and mystring.c, containing the headers and implementations of the following functions....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C programming Write the implementation for the three functions described below. The functions are called...

    In C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. Write a print_string function that prints the characters in a string to screen on- by-one. Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1 if...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • C programming Write the implementation for the three functions described below. The functions are called from...

    C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. a) Write a print_string function that prints the characters in a string to screen on- by-one. b) Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1...

  • USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is...

    USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is one of the simplest encryption techniques in the field of cryptography. It is a cipher in which each letter in a plain text message is replaced by a letter some fixed number of positions up the alphabet (i.e., by right shifting the alphabetic characters in the plain text message). For example, with a right shift of 2, ’A’ is replaced by ’C’, ’B’ is...

  • Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS program that will...

    Please write MIPS program that runs in QtSpim (ex: MipsFile.s) Write a MIPS program that will read in a base (as an integer) and a value (nonnegative integer but as an ASCII string) in that base and print out the decimal value; you must implement a function (which accepts a base and an address for a string as parameters, and returns the value) and call the function from the main program. The base will be given in decimal and will...

  • Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You...

    Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You will place all of your function prototypes in a header file named string utils.h and all of your function definitions in a source file named string utils.c. You should implement your own main test driver program to test your functions, but you need not hand it in. a. void addChar(char *str, char c, int n) - this function should add a char c at...

  • The following code is a C Program that is written for encrypting and decrypting a string....

    The following code is a C Program that is written for encrypting and decrypting a string. provide a full explanation of the working flow of the program. #include <stdio.h> int main() { int i, x; char str[100]; printf("\n Please enter a valid string: \t"); gets (str); printf ("\n Please choose one of the following options: \n"); printf ("1 = Encrypt the given string. \n"); printf("2 = Decrypt the entered string. \n"); scanf("%d",&x); // using switch case statements switch (x) {...

  • Write a function that can return the length of a string in C language. Please leave...

    Write a function that can return the length of a string in C language. Please leave comments to explain the code and verify that it runs properly... Example: Input String: China The length of the string is 5. #include <stdio.h> int length(char * p) {     /write your code here } main(){     int len;     char str[20];     printf("Input string:");     scanf("%s", str);     len = length(str);     printf("The length of string is %d. ", len);     getchar();    ...

  • devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include...

    devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include <ctype.h> #include <termios.h> #include <sys/types.h> #include <sys/mman.h>    #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \ __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0) #define MAP_SIZE 4096UL #define MAP_MASK (MAP_SIZE - 1) int main(int argc, char **argv) { int fd; void *map_base = NULL, *virt_addr = NULL; unsigned long read_result, writeval; off_t target; int access_type = 'w';    if(argc...

  • Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified...

    Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified character in a string) Write a recursive function that finds the number of occurrences of a specified letter in a string using the following function header. int count(const string& s, char a) For example, count("Welcome", 'e') returns 2. Write a test program that prompts the user to enter a string and a character, and displays the number of occurrences for the character in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT