|
-
Feb 15th, 2001, 02:04 PM
#1
Thread Starter
Member
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  )
-
Feb 15th, 2001, 03:34 PM
#2
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;
}
-
Feb 16th, 2001, 04:52 AM
#3
Thread Starter
Member
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  )
-
Feb 17th, 2001, 03:31 PM
#4
Frenzied Member
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.
-
Feb 17th, 2001, 04:01 PM
#5
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
-
Feb 17th, 2001, 05:17 PM
#6
Thread Starter
Member
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  )
-
Feb 17th, 2001, 05:18 PM
#7
Frenzied Member
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Feb 17th, 2001, 05:34 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|