Write a program that can find the length of a string, find the number of words,
ID: 3349361 • Letter: W
Question
Write a program that can find the length of a string, find the number of words, copy a string, and capitalize the copied string Your program will not take any arguments. Instead, we will pass in the string using a built- in function called fgets int main(int argc, char **argv) char string[100]; char string2[100]; // get the input string printf("Enter string: "); fgets(string, 100, stdin); fgets will take a line of input from the user and the resulting characters will be put in the string array. It will also put a 'In' (newline) which you will need to remove from the end of the string array. Make sure you do not count newline when you calculate the en You can find the number of words by counting spaces. You can check if a character is a space by checking if it is equal to 0x20 (the ASCII code for space) or comparing it with '. You will need to handle the case where the user put multiple spaces between words That should not count as multiple words For the string copy, copy the characters from the original string and put it into string2. Make sure you terminate the new string2with a 0 The program results should look like: ./string Enter string: This is my string The length of the string is 17 The number of words is 4 The capitalized string is THIS IS MY STRING The original string is This is my stringExplanation / Answer
#include <stdio.h>
#include<string.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
char string[MAX_SIZE], string1[MAX_SIZE];
int i, words;
/* Input string from user */
printf("Enter string: ");
gets(string);
i = 0;
words = 1;
printf("The Length of string is %d ",strlen(string));
strcpy(string1,string);
/* Runs a loop till end of string */
while(string[i] != '')
{
/* If the current character(str[i]) is white space */
if(string[i]==' ' && (string[i+1]==' '))
{
words++;
i=i+2;
}
if(string[i]==' ' || string[i]==' ')
{
words++;
}
i++;
}
printf("Total number of words is %d ", words);
printf("The Capitalized string is: %s ",strupr(string));
printf("The Original string is: %s ", string1);
return 0;
}
Output:
Enter string: This is Sandeep
The Length of string is 15
Total number of words is 3
The Capitalized string is: THIS IS SANDEEP
The Original string is: This is Sandeep
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.