-
Breaking down a string
HELP PLEASE!!! I am writing a program putting in a street address.
I have to disect the street address and capitalize each word in the street name. I have done this in a loop, using a find for each space, unfortunately the results are not consistent. Sometimes it will work right and others it is like there has been a size specified on each assigned variable. I can't assign a definite size due to changing addresses. I will attach a sample of the code. Please help with any ideas. If there is a better way let me know.
// Get street addresss
cout <<"Please enter your street address (1295 Any Street): " << endl;
getline(cin, StreetAddress);
//Get size of StreetAddress
StreetAddySize = StreetAddress.size();
//Find first blank.
SpaceLocation = StreetAddress.find(" ", 0);
//Assign the street number.
StreetNumber.assign(StreetAddress, 0, SpaceLocation);
// Adds the current SpaceLocation to the TotalSpaces
TotalSpace = TotalSpace + SpaceLocation;
// Loops through to error check each word of the StreetAddress, in cases where there is more than one word.
do
{
//Get location of next blank space.
SpaceLocation = StreetAddress.find(" ", TotalSpace);
//Increment StreetWords.
StreetWords ++;
//First word in StreetAddress.
if (StreetWords ==1)
{
StreetWordOne.assign(StreetAddress, TotalSpace, SpaceLocation);
//Error checking StreetWordOne to make first letter upper case and the rest lower case
transform(StreetWordOne.begin(), StreetWordOne.end(), StreetWordOne.begin(), tolower);
transform(StreetWordOne.begin(), StreetWordOne.begin() + 2, StreetWordOne.begin(), toupper);
TotalSpace = TotalSpace + SpaceLocation;
}
//If a second word in StreetAddress.
else
if (StreetWords ==2)
{
StreetWordTwo.assign(StreetAddress, TotalSpace, SpaceLocation);
//Error checking StreetWordOne to make first letter upper case and the rest lower case
transform(StreetWordTwo.begin(), StreetWordTwo.end(), StreetWordTwo.begin(), tolower);
transform(StreetWordTwo.begin(), StreetWordTwo.begin() + 2, StreetWordTwo.begin(), toupper);
TotalSpace = TotalSpace + SpaceLocation;
}
//If third word in StreetAddress.
else
if (StreetWords ==3)
{
StreetWordThree.assign(StreetAddress, TotalSpace, SpaceLocation);
//Error checking StreetWordThree to make first letter upper case and the rest lower case
transform(StreetWordThree.begin(), StreetWordThree.end(), StreetWordThree.begin(), tolower);
transform(StreetWordThree.begin(), StreetWordThree.begin() + 2, StreetWordThree.begin(), toupper);
TotalSpace = TotalSpace + SpaceLocation;
}
//If more than three words.
else
if (StreetWords > 3)
{
TotalSpace = StreetAddySize;
}
//If no more blank spaces.
else
if (StreetAddress.find(" ", TotalSpace) == -1)
{
TotalSpace = StreetAddySize;
}
}
-
The easiest way is to use strtok found in the string.h header file.
Code:
/*an array of pointers and a single pointer*/
char *address[10], *p;
p = strtok(array, " "); /*the first call determines the string to parse*/
for(int i = 0; i < 10; ++i){
address[i] = new char[50]; /*allocate space for a string*/
strcpy(address[i], p); /*copy the string*/
p = strtok(NULL, " "); /*all subsequent calls to strtok use NULL*/
}
Then you can use toupper() in the ctype.h header file on the first element of each pointer in address to make them uppercase. Be sure to check that they're letters first though. ;)
-Prelude
-
Thank you very much Prelude.