Thread: string manipulation

  1. #1
    Registered User
    Join Date
    May 2005

    string manipulation

    Hello All,

    I apologize if these questions sound lame, I have some C++ experience, but, not much with string manipulation. I have a large comma delimited text file and I need to modify some data that it contains.

    Some records have malformed phone numbers with random text appearing in the column. I would like to to force the phone numbers into the following format: "(555)-555-5555" and also eliminate any unecessary data that appears after 5555. Could anyone point me in the right direction?

    I have looked at using GRETA to implement regular expression string matching/substitution, but I cannot figure out how to use it. Do I need to compile it as an additional library or something like that? Would anybody have any instructions for using it? The html file that's included only covers some basic syntax, function and class information.

    Thanks in advance for any help or information, I really appreciate it.


  2. #2
    Registered User
    Join Date
    Apr 2003
    Regular expressions are great but you can also use string functions.

    1) You can use:

    string str;
    getline(yourInputFile, str, ',') ;

    to read each chunk of data between the commas. That will read data up to the next comma and discard the comma, leaving you with only the data.

    2) You can access the characters in a string like an array, so you can read one character at a time in a loop using:


    3) You can assign the char str[i] to an int variable, which will convert it to its ascii code. If the ascii code is between 48 and 57, then you have a digit. That should enable you to pick out all the digits in a chunk of data. First declare a string variable:

    string input = "(";

    Then, if the first character in your chunk of data is a digit, add it to your string, else continue the loop and get the next character in the string. When, a found digit counter reaches 3, add ")-" to the string and continue.

    4) Create a master string and add each individually formatted string to it. When you've read the whole file in, close the file, open the file for writing--which will erase everything in there--and write the master string to the file.

    You should be able to write the whole thing using 20-30 lines of code.
    Last edited by 7stud; 05-31-2005 at 02:30 AM.

  3. #3
    Registered User
    Join Date
    May 2005

    String Manipulation - Thanks

    Thank you so much for your help I will give this a shot when I get to work.


  4. #4
    Registered User
    Join Date
    Jun 2004
    probably regular expressions is a bit over your head.. and very laborous..! yet a very cool subject..! i think i'll work on something about that in the future...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. string manipulation
    By SPEKTRUM in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:41 AM