I'm not sure if I'm doing this part correctly, the part where I have to make it all lower case then to all upper case, then just first name.

#include <iostream>
#include <cstring>
#include <stdio.h>

using namespace std;

int main()
{
    char a[3][14] = { "Barack ", "H", " Obama" };

    cout << "Enter your full name: ";
    cout << a[0] << a[1] << a[2] << endl;

    //Find length of name
    int i = strlen(a[0]);
    int j = strlen(a[1]);
    int k = strlen(a[2]);
    cout << "\tYou name's length is " << (i+j+k) << endl;

    tolower (a[0][0]); 
    tolower (a[0][1]);
    tolower (a[0][2]);
    tolower (a[0][0]);

    cout << "\tYour name in lower case: " << a[0][0] << a[1] << a[2] << endl;

    cout << "\tYour name in upper case: " << a[0] << a[1] << a[2] << endl;

    system("pause");
    return 0;
}

Output should look like:

Enter your full name: Barack H Obama
    You name's length is 14
Your name in lower case: barack h obama
Your name in upper case: BARACK H OBAMA
Your first name: BARACK

BARACK, what is your friend's name? barack
    Your name is the same as your friend's name

Dani AI

Generated

Quick, practical follow-up for (and thanks for the pointer): treat the full name as a single string if you can — it makes length, case changes and extracting the first name much simpler. Use std::string, std::getline to read the whole line, then make lowercase/uppercase copies with std::transform. If you must keep the three-element char[][], either concatenate them into one std::string first or apply the same per-character transform to each element.

Here is a compact, safe example that shows the steps (read, length, lower, upper, first name):

#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main() {
    std::string name;
    std::cout << "Enter your full name: ";
    std::getline(std::cin, name);

    std::cout << "\tYou name's length is " << name.size() << "\n";

    auto lower = name;
    std::transform(lower.begin(), lower.end(), lower.begin(),
                   [](unsigned char c){ return static_cast<char>(std::tolower(c)); });
    std::cout << "\tYour name in lower case: " << lower << "\n";

    auto upper = lower;
    std::transform(upper.begin(), upper.end(), upper.begin(),
                   [](unsigned char c){ return static_cast<char>(std::toupper(c)); });
    std::cout << "\tYour name in upper case: " << upper << "\n";

    auto pos = name.find(' ');
    std::string first = (pos == std::string::npos) ? name : name.substr(0, pos);
    std::transform(first.begin(), first.end(), first.begin(),
                   [](unsigned char c){ return static_cast<char>(std::toupper(c)); });
    std::cout << "\tYour first name: " << first << "\n";
}

A few extra tips: always cast to unsigned char before calling std::tolower/std::toupper to avoid undefined behavior on negative char values; these functions return an int, so assign the result back to the character. For case-insensitive comparisons (friend name), transform both strings to the same case and then compare. If you stick with your a[3][14] approach, either sum strlen (as you did) or loop each element and assign converted chars back — do not call tolower/toupper and ignore the return value.

Recommended Answers

All 3 Replies

The tolower and toupper functions only work on a single character at a time. So, if you want to apply it to a whole string, then you need to iterate through the characters in the string and apply that function to each character.

For example, if you have a string, you can do this:

char a[] = "Something";
int a_n = strlen(a);
for(int i = 0; i < a_n; ++i)
  a[i] = char(tolower(a[i]));

This will turn each character of the a string into a lower-case one. You should be able to figure out how to apply that to your particular problem.

Generally-speaking though, it is easier to use the C++ class std::string instead of these old C-style strings (char*).

so should I delete this part? :

 //Find length of name
    int i = strlen(a[0]);
    int j = strlen(a[1]);
    int k = strlen(a[2]);
    cout << "\tYou name's length is " << (i+j+k) << endl;
    tolower (a[0][0]); 
    tolower (a[0][1]);
    tolower (a[0][2]);
    tolower (a[0][0]);

But it has three elements though, how would that be incorporated?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.