Question

You will implement some helpful string functions. The prototypes are below. The only built-in functions you...

You will implement some helpful string functions. The prototypes are below. The only built-in functions you can use are toupper() and tolower() – found in ctype.h.

char* capitalize(char* s);
char* str2Lwr(char* s);
char* str2Upr(char* s);

All of the functions should be "in-place". In-place means that you should update the string pointed to by s, rather than creating a new one.

capitalize() should convert the first character of string s to upper case. The value of s is returned.

str2Lwr() should convert the entire string s to lower case. The value of s is returned.

str2Upr() should convert the entire string s to upper case. The value of s is returned.

If the character to be converted does not have a "case", mimic the tolower() and toupper() functions.

In main, define and initialize strings to test your functions. Call your functions with the strings you initialized to verify the functions are implemented correctly.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ program:

StringCaseConversion.cpp:

#include <iostream>
#include <string.h>
using namespace std;

char* capitalize(char* s)
{
    s[0]=toupper(s[0]);//capitalizes the first letter in string
    return s;
}
char* str2Lwr(char* s)
{
    int i=0;
    for(i=0; i<strlen(s); i++)
        s[i] = tolower(s[i]);//takes the string char by char and converts to lower case
    return s;
}
char* str2Upr(char* s)
{
    int i=0;
    for(i=0; i<strlen(s); i++)
        s[i] = toupper(s[i]);//takes the string char by char and converts to upper case
    return s;
}
int main() {
    char *x = strdup("shyam");
    char *y = strdup("cheGG");
    char *z = strdup("c++");
    char *a = strdup("prograM");

    cout<<"Capitalizing the words: ";
    cout<<endl;
    cout<<capitalize(x)<<endl;
    cout<<capitalize(y)<<endl;
    cout<<capitalize(z)<<endl;
    cout<<capitalize(a)<<endl;
    cout<<endl<<endl;
    cout<<"Converting all letters to lower case";
    cout<<endl;

    cout<<str2Lwr(x)<<endl;
    cout<<str2Lwr(y)<<endl;
    cout<<str2Lwr(z)<<endl;
    cout<<str2Lwr(a)<<endl;
    cout<<endl<<endl;

    cout<<"Converting all letters to UPPER case";
    cout<<endl;
    cout<<str2Upr(x)<<endl;
    cout<<str2Upr(y)<<endl;
    cout<<str2Upr(z)<<endl;
    cout<<str2Upr(a)<<endl;
}


Output:

Add a comment
Know the answer?
Add Answer to:
You will implement some helpful string functions. The prototypes are below. The only built-in functions you...
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
  • 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...

  • Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a...

    Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a function that finds if a given value is present in a linked-list. The function should look for the first occurrence of the value and return a true if the value is present or false if it is not present in the list. Your code should work on a linked-list of any size including an empty list. Your solution must be RECURSIVE. Non-recursive solutions will...

  • In C Programming Language In this lab you will implement 4 string functions, two using array...

    In C Programming Language In this lab you will implement 4 string functions, two using array notation and two using pointers. The functions must have the signatures given below. You may not use any C library string functions. The functions are 1. int my strlen (char s ) - This function returns the number of characters in a string. You should use array notation for this function. 2. int my strcpy (char s [], char t I)- This function overwrites...

  • C++ LANGUAGE Using only pointer notation, implement and test the following functions that operate on C...

    C++ LANGUAGE Using only pointer notation, implement and test the following functions that operate on C Strings: char *strStr(const char *s1, const char *s2) - Locates the string s2 as a substring of s1 and returns a pointer to its first occurrence in s1 or nullptr if s2 is not a substring of s1 char *strrChr(constant char *s, int ch) - Returns a pointer to the last occurrence of the character ch in string s or nullptr if ch is...

  • Intro to Programming in C – Large Program 1 – Character/ Number converter Assignment Purpose: To...

    Intro to Programming in C – Large Program 1 – Character/ Number converter Assignment Purpose: To compile, build, and execute an interactive program with a simple loop, conditions, user defined functions and library functions from stdio.h and ctype.h. You will write a program that will convert characters to integers and integers to characters General Requirements • In your program you will change each letter entered by the user to both uppercase AND lowercase– o Use the function toupper in #include...

  • Our lab today revolves around Character Strings (C-Strings) which are essentially character arrays. We’ll be writing...

    Our lab today revolves around Character Strings (C-Strings) which are essentially character arrays. We’ll be writing two functions that will output the number of vowels and consonants in a user inputted string. We’ll be using functions from the cstring library, so be sure to include it! We’ll be calling those functions method_one and method_two. To start off, declare a character array (with no size) called vowels and initialize it with “aeiouyAEIOUY” in our main function. Declare a character array with...

  • C++ PROGRAMMING Implement a bool function whose header is given below. Inside the function, declare, ask,...

    C++ PROGRAMMING Implement a bool function whose header is given below. Inside the function, declare, ask, and get a character from the user. The function will return true if the character the user enters is NOT in either of the words, otherwise it will return false. Keep in mind that uppercase and lowercase letters are not the same. You will need to change both words and the character input by the user to the same case (upper or lower) -...

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

  • In this lab, you will need to implement the following functions in Text ADT with C++...

    In this lab, you will need to implement the following functions in Text ADT with C++ language(Not C#, Not Java please!): PS: The program I'm using is Visual Studio just to be aware of the format. And I have provided all informations already! Please finish step 1, 2, 3, 4. Code is the correct format of C++ code. a. Constructors and operator = b. Destructor c. Text operations (length, subscript, clear) 1. Implement the aforementioned operations in the Text ADT...

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