Get first letter of string, determine if lowercase.

Any way to pull the first letter of a string, and then determine, through science, whether or not its lower case so I can then apply C++ magics.

i know about the islower()

i tink it has to be a Char type. so i need to take my string and pull the first Char out of it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
   
  string myName;
  char c;
  
  cout << "Enter name:  " ;
  
  getline(cin, myName);

  // now somehow get variable c from string myName
  
 // then check if c islower()

 // then replace c with upper
 ?? then put back into string...aaaaargh


You can get the first letter of a string with char firstLetter = myName.at(0); or use the [] operator if you prefer.

You don't need to check if it is lower case - just use the toupper function from <cctype>
i need to take myName and search for it in a file, so i need to somehow put that newly upper cased letter back into the original string and then use that to perform the search.

How do i put it back/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <cctype>

int main()
{
    std::string my_name ;
    std::cout << "enter name: " ;
    std::getline( std::cin, my_name ) ;

    if( !my_name.empty() )
    {
        // if first letter of my_name is a lower case character, convert it to upper case
        my_name.front() = std::toupper( my_name.front() ) ;

        // note: you may ant to trim leading white space characters
        //       and adjust the case of other characters of the name. for instance:
        //       " gaBriel garCía márQuez" => "Gabriel García Márquez"

        std::cout << my_name << '\n' ;
        // search for name in a file
    }
}
The following should do the job:
myName[0] = toupper(myName[0]);
here's how i ultimately did it:

edit:: thanks for all the help;


1
2
3
4
5
6
7
cout << "Enter name:\n>";

	getline(cin, searchName);
	char firstLetter = searchName.at(0);
	//putchar (toupper(firstLetter)); // this reverts when reinserted for some reason;
	searchName.erase(0,1);
	searchName.insert(0,1,toupper(firstLetter));
Last edited on
Topic archived. No new replies allowed.