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

There are two parts A. Write a C++ function that changes a C String (parameter)

ID: 673409 • Letter: T

Question

There are two parts

A. Write a C++ function that changes a C String (parameter) to all UPPER CASE. Assume the null char '' is after the last char used in the C string, so this function will NOT use a SIZE variable nor SIZE const. You'll have to convert one char at a time (use toupper). THE ONLY PARAMETER WILL BE THE C string (array of chars), NOTHING ELSE!

B. Write a C++ function that changes a C++ string (parameter) to all UPPER CASE. This function will NOT use a SIZE variable nor SIZE const, but must use the string member function that gives how many chars are in the string. You'll have to convert one char at a time (use toupper). THE ONLY PARAMETER WILL BE THE C++ string, NOTHING ELSE! (DON'T CALL THE FUNCTION FOR A. IN THIS FUNCTION!)

If time, test these functions in a C++ program with a C string variable and a C++ string which have some lowercase letters, uppercase letters and other chars. Print the results of each string after each function was called.

Explanation / Answer

Difference between C string and c++ string

C-strings (#include <cstring>)         C++ strings (#include <string>)       

Declaring a C-string variable           Declaring a C++ string object

char str[30];                           string str;

Initializing a C-string variable        Initializing a C++ string object

char str1[11] = "Chegg";           string str1("Chegg India!");

char str2[] = "Send gold!";            string str2 = "Send Bat!";

char str3[] = {'O', 'K', ''};         string str3("OK");

Last line above has same as:

char str3[] = "OK";

                                        

                                       

Assigning to a C-string variable        Assigning to a C++ string object

     

Can't do it, i.e., can't do this:       string str;

char str[10];                           str = "Hello";

str = "Hello!";                         

Concatenating two C-strings             Concatenating two C++ string objec

strcat(str1, str2);                     str1 += str2;

strcpy(str, strcat(str1, str2));        str = str1 + str2;

Copying a C-string variable             Copying a C++ string object

char str[20];                           string str;

strcpy(str, "Hello!");                  str = "Hello";

strcpy(str, otherString);               str = otherString;

Accessing a single character            Accessing a single character

str[index]                              str[index]

                                        str.at(index)

                                        str(index, count)

                                       

Comparing two C-strings                 Comparing two C++ string objects

if (strcmp(str1, str2) < 0)             if (str1 < str2)

    cout << "str1 comes 1st.";              cout << "str1 comes 1st.";

if (strcmp(str1, str2) == 0)            if (str1 == str2)

    cout << "Equal strings.";               cout << "Equal strings.";

if (strcmp(str1, str2) > 0)             if (str1 > str2)

    cout << "str2 comes 1st.";              cout << "str2 comes 1st.";

   

Finding the length of a C-string        Finding the length of a C++ string object

strlen(str)                             str.length()

String_Upper_cpp.cpp

#include <iostream>/*cin and cout functions are present*/

#include <algorithm>/* Transform function used in this header file*/

#include <string> /*contains string functions*/

#include <cstring>/* This header file of C strings and arrays.*/

#include <functional> /*This header is part of the function objects library */

#include <cctype>/*it contain functions like upper,isalpha etc*/

using namespace std;

int main()

{

    char arr[20] ;/*C String is represented using char array*/

string s(arr);/*converting char array into string in cpp*/

    cout<<"enter the string in lower case ";

    cin >> s;

    std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun<int, int>(std::toupper));

   cout<<" string in upper case ";

    cout << s;

    cout<<"length of the string ";

    cout << s.length();

    return 0;

}

Output

Enter the string in lower case cheggindia

String converted into uppercase CHEGGINDIA

Length of the string 7

StringUpperc.CPP

#include <iostream>

#include <algorithm>

#include <string>

#include <cstring>

#include <functional>

#include <cctype>

using namespace std;

int main()

{

    char arr[20] ;/*C String is represented using char array*/

string s(arr);/*converting char array into string in cpp*/

    cout<<"enter the string in lower case ";

    cin >> s;

    std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun<int, int>(std::toupper));

    cout << s;

    return 0;

}

Output

Enter the string in lower case cheggindia

String converted into uppercase CHEGGINDIA