Thread: Finding the case of a string

  1. #1
    [root@matrix]# NullStyle's Avatar
    Join Date
    Nov 2003
    Posts
    7

    Finding the case of a string

    Hi guys. If I have a vector that stores string inputs and I am supposed to display the output of lower cased strings before the upper cased strings, how can I know whether the first letter of the string is an upper case or lower case letter? Thanks in advance.

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  3. #3
    [root@matrix]# NullStyle's Avatar
    Join Date
    Nov 2003
    Posts
    7
    Sorry if I forgot to specify that I use the string type, not the char type. If I am not wrong, the two functions don't work for the string type. How can I find the case of the first letter of a string?

  4. #4
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    to access the first character of a std::string:
    Code:
    std::string title = "C++ Programmer";
    cout << title[0] << endl;
    the output is 'C'

    new to change it to lowercase:
    Code:
    title[0] = tolower(title[0]);
    same applies to the rest of the string characters.

  5. #5
    [root@matrix]# NullStyle's Avatar
    Join Date
    Nov 2003
    Posts
    7
    I appreciate your help, but is there a function that can find out whether the first character of a string is in uppercase or lowercase without displaying it using cout?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Look back at the hints.
    Do they apply to C strings, or to characters?

  7. #7
    [root@matrix]# NullStyle's Avatar
    Join Date
    Nov 2003
    Posts
    7
    Of course they apply to characters.
    My problem is that I have a program that allows users to enter input in the form of the std::string type and storing them in a vector. The output begins with lowercase strings, then uppercase strings. I tried using the sort and substr functions but the result is not what I have expected. I tried using substr to find the first character of the strings but it doesn't work with the isupper and islower functions as the substr function returns a string. How can I know whether the first character of the strings are uppercase or lowercase characters? Thanks in advance.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    1. You have isupper() and islower(), which accepts character arguments.

    2. You can use str[0] to access the 1st character in str, where str is of type std::string (or char*, for that matter)

    3. You need to know if the 1st character in str is uppercase or lowercase.

    The logical conclusion then is to use isupper(str[0]), and possibly islower(str[0]), though if your input is purely alphabetic you only need use one to know the other.

  9. #9
    [root@matrix]# NullStyle's Avatar
    Join Date
    Nov 2003
    Posts
    7
    Thanks a lot. I tried out this approach, but in another way. I stored the value of str[0] in another string and got errors, which is possibly why it didn't work and I didn't bother to use str[0] as an argument to islower and isupper. I didn't know that taking a character from a string is still a valid argument for a function that requires a char. Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 02:07 AM