Question

Objectives: Use strings and string library functions. Write a program that asks the user to enter...

Objectives:

Use strings and string library functions.

Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions:

A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]);

which tell you how many spaces are in the string. The \0’ is still at the end of the string, so the loop control will be same.

1. A function called to_uppercase that converts all the characters in a string to uppercase letters.

     void to_uppercase(char str[]);

Now your program will run something like this:

Enter a string:
A C string is terminated by a null character.
String Entered:
A C STRING IS TERMINATED BY A NULL CHARACTER.
The string has 8 white spaces.

Since we are passing the actual array to the function and not making a copy, any changes that we make to the array happen directly in the original array that was passed in.

Hints:

  • Make sure to include <string.h>

  • Use the function fgets to read a string into an array of characters.

  • Remove the end-of-line character ('\n') from the string by replacing it with the null

    character ('\0')

  • If the user hits the enter key without typing anything, then after removing the '\n',

    the string will be empty.

  • Use the function strlen to test if the string is empty.

            if (strlen(line) == 0)
    
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program: #include<stdio.h> #include<string > int count space (char str[]) int lengthstrlen (str) int count = 0 foc (1 = 0; i<Sample output: clusers 203470 documentsivisual studio 20151Projects Project1351Debug Project135.exe Enter a String:A C stringEditable code:

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

int count_space(char str[])
{
   int length = strlen(str);
   int i, count = 0;
   for (i = 0; i<length; i++)
       if (str[i] == ' ')
           count++;
   return count;
}

void to_uppercase(char str[])
{
int c = 0;

while (str[c] != '\0')
{
if (str[c] >= 'a' && str[c] <= 'z')
{
str[c] = str[c] - 32;
}
c++;
}
}
int main()
{
   char str[100];
   int W = 0, i;
   printf("\nEnter a String:");
   fgets(str, 100, stdin);
   if (strlen(str) == 0)
   {
       printf("Enter the string whose length greater than 0");
   }

   printf("String Entered: \n");
   int val = count_space(str);
   to_uppercase(str);
   printf("\n%s", str);
   printf("\nThe string has %d white spaces\n", val);
  
   system("pause");
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Objectives: Use strings and string library functions. Write a program that asks the user to enter...
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 language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • MIPS programming question Problem 1: Write a program that asks the user to input a string...

    MIPS programming question Problem 1: Write a program that asks the user to input a string (or no more than 50 characters). Your program should then output the length of the string. The string length should be determined using a separate function strlen that will accept the address of the string and return its length. For the purposes of this exercise, the length of a string will be defined as the number of non-null and non-newline characters until either the...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

  • Write a C program that takes two sets of characters entered by the user and merge...

    Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...

  • I'm having trouble getting my program to output this statement Enter a sentence: Test! There are...

    I'm having trouble getting my program to output this statement Enter a sentence: Test! There are 5 total characters. There are 1 vowel. There are 1 UPPERCASE letters. There are 3 lowercase letters. There are 1 other characters. Here's my code: #include<string.h> #include<stdio.h> #include<stdbool.h> int main() { char str[100]; int i; int vowels=0; int UC; int LC; int Others; int c; printf("Enter a sentence: \n"); gets(s); LC=countLC(&s); UC=countUC(&s); Others=countOthers(&s); printf("There are %d total characters.\n", ; for(i=0; i<strlen(str); i++){ if(isVowel(str[i])) vowels++;...

  • I was asked to write a program that when divisible by 2- reverses the order of...

    I was asked to write a program that when divisible by 2- reverses the order of the letters in a sentence when divisible by 3- changes all lowercase letter into uppercase letters in a sentence when divisible by 5 skips the vowels in a sentence this is what I have so far. my reverseMode works fine. #include <iostream> #include <string> #include<cstring> using namespace std; void reverseMode(char* str); void upperMode(char* str); void goodbyeVowels(char* str); char str[50], rstr[50]; //initializing my character arrray...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • Prompt the user to enter two character strings. The program will then create a new string...

    Prompt the user to enter two character strings. The program will then create a new string which has the string that appears second, alphabetically, appended to the end of the string that shows up first alphabetically. Use a dash (-') to separate the two strings. You must implement the function string appendStrings(string, string), where the return value is the combined string. Display the newly created string. Your program must be able to handle both uppercase and lowercase characters. You are...

  • Write in C Spring 2016 Lab Assignment 11 ET2100 In computer programming in general a "string"...

    Write in C Spring 2016 Lab Assignment 11 ET2100 In computer programming in general a "string" is a sequence of characters. In the C language anything within double quotes is a "string constant" so you have been seeing strings all semester. But we can also have string variables. In the C language these are implemented as an array of char, e.g. char name (10]: In order to make these variables easier to work with, it has been universally agreed that...

  • 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...

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
ADVERTISEMENT