Parsing strings

This is a discussion on Parsing strings within the C++ Programming forums, part of the General Programming Boards category; I have a string of letters seperated by periods, like this: a.ds.ees.f.csg.cw.s.ab.s.f.s.ffsd.j I want to place each set of letters ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    22

    Parsing strings

    I have a string of letters seperated by periods, like this:
    a.ds.ees.f.csg.cw.s.ab.s.f.s.ffsd.j
    I want to place each set of letters into a element of an array, so it would look something like:
    [a][ds][ees][f][csg][cw][s][ab][s][f][s][ffsd][j]
    Can you guys give me an idea on how to do this.

    Thanks

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,688
    Loop through each character, appending it to the array item in queue, then if you hit a period, queue the next array item.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    int main ()
    {
       std::string input;
       std::vector<std::string> ar;
       while ( std::getline( std::cin, input, '.' ) ) {
          ar.push_back( input );
       }
    }
    With my apologies, I'm quite aware you aren't allowed to use the STL if you are doing homework. But you could borrow some of the ideas and write something similar to std::getline.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    22
    Thanks for the idea.
    Now what is wrong with program it crashes;

    Code:
        vector <string> text; 
    
       string input = "a.ds.ees.f.csg.cw.s.ab.s.f.s.ffsd.j"  ;
     
       int j = 0;
       for (int i=0;i < input.length(); i++){
             if(isalpha(input[i]))
                    text[j] += input[i];
             else if(ispunct(input[i]))
                     j++;
       }
    
     for (int i=0;i < text.size(); i++)
         cout  <<"[" << text[i] << "]"<< endl;

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to use put_back("") somewhere before you access element j - or set the size of the vector before-hand.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    22
    but when I do that it places every char into a seperate element, and I want to group them

    insted of [a][d][s][e][e] ... I want [a][ds][ees] ...

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Tupcia View Post
    but when I do that it places every char into a seperate element, and I want to group them

    insted of [a][d][s][e][e] ... I want [a][ds][ees] ...
    You are probably not placing your "put_back()" in the right place, then.

    Edit: And if you can't figure out where it should go, you need to post your code (so that we can see what you are currently doing, and fix it up).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    22
    Code:
        vector <string> text; 
    
       string input = "a.ds.ees.f.csg.cw.s.ab.s.f.s.ffsd.j"  ;
     
       int j = 0;
       for (int i=0;i < input.length(); i++){
             if(isalpha(input[i]))
                    text.push_back(input[i])
             else if(ispunct(input[i]))
                     j++;
       }
    This does not even compile, it does when I make text a vector of chars, but then eeach element is just 1 letter.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you have a vector of strings, then the things you push back must be strings. input[i] is not a string, it's a letter. You need to build a string -- look into substr.

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    22
    Got it, by breaking the string into tokens:

    Code:
       
     vector <string> text; 
    
       char input = "a.ds.ees.f.csg.cw.s.ab.s.f.s.ffsd.j"  ;
     
       char *a;
       a = strtok(input, ".");
       while (a != NULL) {
          text.push_back(a);
          a = strtok(input, "."); 
       }
    Thanks for all the help !

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    That won't compile unless you make input a char array or const char pointer.

    And if you were ever doing this outside of class, prefer citizen's implementation.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Registered User asbo60's Avatar
    Join Date
    Jan 2006
    Posts
    38
    I'm not a very experienced programmer but couldn't you just use scanf?
    Come give my Website a visit and try my new program, all you gamers will love it!
    www.AzA-Productions.com
    Click on applications and download Key Control!

    Also I have tons of extra space available so, if you have a project or application you want the world to see just give me an email at admin@aza-productions.com and ill see what i can do for you.

    Asbo60

  13. #13
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    just for fun i did it this way.

    Code:
            char *input = "a.ds.ees.f.csg.cw.s.ab.s.f.s.ffsd.j"  ;
            unsigned int length = strlen(input);
            char delimiter = '.';
            unsigned int numFields=0;
            unsigned int *delimiterPositions=new unsigned int[1];
            for(int i=0;i<length;i++)
            {
                    if(input[i]==delimiter)
                    {
                            numFields++;
                            realloc(delimiterPositions,numFields*sizeof(unsigned int));
                            delimiterPositions[numFields-1]=i;
                    }
            }
            char **parsed = new char*;
            unsigned int start = 0;
            unsigned int end;
            for(int i=0;i<numFields;i++)
            {
                    end = delimiterPositions[i];
                    parsed[i]=new char[end-start];
                    memcpy(parsed[i],input+start,end-start);
                    start = end+1;
            }
    now, i originally used realloc on parsed, but after several successful iterations, i get an error. any ideas?

    Code:
            char *input = "a.ds.ees.f.csg.cw.s.ab.s.f.s.ffsd.j"  ;
            char **parsed = new char*;
            char delimiter = '.';
            unsigned int numFields=0;
            char *start = input;
            char *end= (char *)memchr(input,delimiter,strlen(input));
            while( end!=NULL )
            {
                    numFields++;
                    realloc(parsed,numFields*sizeof(char *));
                    char *end= (char *)memchr(start,delimiter,strlen(start));
                    int size = end-start;
                    parsed[numFields-1]=new char[end-start];
                    memcpy(parsed[numFields-1],start,end-start);
                    start = end+1;
    
            }
    Last edited by m37h0d; 04-24-2008 at 01:18 AM.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Hello World, this is C++, not C.
    Let's get rid of unsafe stuff such as memcpy and get rid of new so you don't have to delete.
    Citizen already provided an implementation. Does it not work?

    m37h0d, your code never frees the memory you allocate.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by Elysia View Post
    Hello World, this is C++, not C.
    Let's get rid of unsafe stuff such as memcpy and get rid of new so you don't have to delete.
    Citizen already provided an implementation. Does it not work?

    m37h0d, your code never frees the memory you allocate.
    well of course not. you'd free it when you didn't need it anymore!


    i'm just asking. there are PACs out there that are programmable in C, but not C++. i realize this is the C++ forum, but i didn't think the topic warranted a whole new thread over there.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parsing command line strings
    By John_L in forum C Programming
    Replies: 15
    Last Post: 05-28-2008, 09:26 AM
  2. sscanf and parsing strings
    By jco1323 in forum C Programming
    Replies: 4
    Last Post: 02-20-2008, 06:32 PM
  3. Parsing Strings
    By Hunter2 in forum C++ Programming
    Replies: 29
    Last Post: 12-05-2004, 07:48 PM
  4. Parsing Strings
    By SubLogic in forum C++ Programming
    Replies: 15
    Last Post: 01-07-2003, 11:11 AM
  5. Searching and Comparing Strings Using Parsing
    By niroopan in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2002, 11:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21