Thread: Breaking down a string

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    3

    Angry 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;
    }
    }
    Last edited by Tina; 12-05-2001 at 12:27 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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
    Last edited by Prelude; 12-05-2001 at 06:03 AM.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    3

    Smile

    Thank you very much Prelude.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. String Class Operations
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 04-06-2003, 02:29 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM