Results 1 to 8 of 8

Thread: Capitalising first letter of Strings

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2000
    Posts
    44
    OK, I'm a newbie C++ programmer. I know there are two ways of storing strings. Either for example, char name[10] or string name (if you include string.h). To capitalise the first letter of any given string in the first example would be easy since you just need to access name[0], but what about in the second example? Does anyone know of any downloadable C++ function references - preferably easy to search/navigate - I'm using the DJGPP compiler and Info aint the easiest thing to get around!

    Thanks in advance

    Matt
    VB5 Enterprise, C++Builder 5, JBuilder 3.5 (so far unused )

  2. #2
    Guest
    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	string str = "hello world";
    	int l = str.length() - 1;
    	char *first = (char*)str.substr(0,1).c_str();
    	if(first[0] >= 'a' && first[0] <= 'z')
    		first[0] = first[0] - 32;
    	String back = str.substr(1, l);
    	str = first;
    	str += back;
    	cout << str;
    	return 0;
    }

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2000
    Posts
    44
    Actually I found an answer yesterday going through some Borland help files:

    Code:
    #include <string>
    #include <iostream>
    
    string name;
    cout << "Please enter your first name";
    cin >> name;
    
    char initial;
    initial = name.at(0); // retrieves first character
    
    If (initial >='a' && initial <='z')
        name.replace(0,1,intital - 32)
    
    cout >> name;
    Thanks anyway.

    Matt
    VB5 Enterprise, C++Builder 5, JBuilder 3.5 (so far unused )

  4. #4
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    I believe it would be faster to Xor the character with the value 0x20 (2^5), this will (i believe it's called) shift (I mean witch between 1 and 0) the 6th bit, which will turn the character either Uppercase if it was Lowercase, and Lowercase to uppercase if it was lowercase..
    confused yet?

    Code:
    #include <string>
    #include <iostream>
    
    string name;
    cout << "Please enter your first name";
    cin >> name;
    
    char initial;
    initial = name.at(0); // retrieves first character
    
    If (initial >='a' && initial <='z')
        name.replace(0,1,intital ^ 0x20) //I believe ^ is for Xor?
    
    cout >> name;
    would be slightly faster/cooler I think
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  5. #5
    Guest
    Shifting isn't quite the same thing.

    Bit Shift Left:
    int x = 5; //5 in binary = 101
    x = x << 1; //x now equals 10, 10 in binary is 1010
    Bit Shift Right:
    int x = 5; //5 in binary = 101
    x = x >> 1; //x now equals 2, 2 in binary is 10
    shift left = add a bit to the end, the bit is always 0.
    shift right = remove a bit.

    You can also shift different numbers of bits:

    int x = 5; //5 in binary = 101
    x = x << 2; //x now equals 20, 20 in binary is 10100
    int x = 5; //5 in binary = 101
    x = x >> 2; //x now equals 1, 1 in binary is 1

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2000
    Posts
    44
    Thanks for the replies, the help is greatly appreciated. Firstly Jop, I only need to swap from lower to uppercase and not the other way around as well. Secondly, in the code segment i gave you for my solution to it I called the function .at(0) once putting the result in a variable. Instead of doing this I could of course have repeated the function call everywhere where I used the name of the variable. This lead me to thinking about performance/optimising programs in terms of a) number of instructions carried out and b) memory usage. Are there any freeware programs that can tell me what the memory usage/processor time taken of my programs is?
    VB5 Enterprise, C++Builder 5, JBuilder 3.5 (so far unused )

  7. #7
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Thanks Dennis, I knew it wasn't shifting what I meant

    btw: if you use Copy & paste, be sure to modify the parts you copied

    Code:
    x = x << 2; //x now equals 20, 10 in binary is 10100 
    ...
    x = x >> 2; //x now equals 1, 2 in binary is 1
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  8. #8
    Guest
    thanks Jop. It's all fixed now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width