Thread: quick question

  1. #1
    Unregistered
    Guest

    Cool quick question

    Is there any functions in the stdlib to convert strings to lower or uppercase ??


    I know there is tolower() and toupper() for char.


    Is it best to just use a char array instead of strings and work through the array in a for loop ??

    Does everyone out there use String or char[index] ??

    Thanks

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    AFAIK you'll have to loop through the strings. It's pretty much the same process for character arrays or C++ strings -

    Code:
    #include <iostream> 
    #include <algorithm>
    #include <string> 
    #include <cstdlib>
    using namespace std;
    
    void mytoupper(char& c)
    {
    	c-=32;
    }
    int main() 
    { 
    
    	string name1 = "zen";
    	char name2 []= "zen";
    
    	//c string
    	for_each(&name2[0],&name2[strlen(name2)],mytoupper);
    
    	//c++ string
    	for_each(name1.begin(),name1.end(),mytoupper);
    
    	cout << name1 << endl << name2;
    
    	return 0; 
    }
    You could use normal for loops and the standard library toupper (I was bored).
    zen

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    that wont work if you pass in "ZEN" or "Zen" or anything with an uppercase char in it... you need to this:

    void MyToUpper(char& c)
    {
    c &= ~0x20;
    }

    void MyToLower(char& c)
    {
    c |= 0x20;
    }

    This will make sure that it works no matter what case is passed in

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  4. #4
    Unregistered
    Guest
    OK I know Uraldor' code works a treat, but I don't understand how it works ?

    Can anyone explain how the code works ?

    Ta

  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    In ASCII the difference between the higher and lower case characters is that the lower case characters have the fifth bit set and the higher case don't. 0x20 (32 = 2^5) is the fifth bit, so Uraldors function removes that bit in toupper and sets it in tolower, using 0x20 and ~0x20 (0xDF) as masks.
    zen

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Some compilers have strupr and strlwr in <string.h> or <cstring>. It may not be part of the standard though.

    char str[31];
    cout << "Enter a string:";
    cin >> str;
    strupr(str);
    cout << "str:" << str << endl;
    Last edited by swoopy; 12-19-2001 at 01:30 PM.

  7. #7
    Unregistered
    Guest
    void AllUpperString(char* input)
    {
    for(i = 0; i < strlen(input); i++)
    {
    input[i] = toupper(input[i]);
    }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM