Thread: C String Manipulation

  1. #1
    Registered User
    Join Date
    Nov 2011

    C String Manipulation

    So I have a string containing student information which I've scanned in from a text file. Originally it looked something like this:

    3800 Victor Lee; 2.8
    3000 Joanne Brown; 4.0
    1300 Frankie South; 2.6

    I scanned them in using:
    char** buildList (int size)
        char** list;
        char temp[MAX_SIZE];
        char* pFirst, *pLast;
        if(!(list = (char**)calloc (size + 1, sizeof(char*))))
        while((fgets(temp, sizeof(temp), fp1)) != NULL)
            pFirst = temp + 5;
            pLast = strchr(temp, ';');
            *pLast = '\0';
             strcpy(temp, pFirst);
             strcat(temp, ",");
            fputs(temp, stdout);
            fputs("\n", stdout);
    My output now looks like:

    Victor Lee,
    Joanne Brown,
    Frankie South,

    Ultimately, it is supposed to resemble the format:

    Lee, Victor

    all other information is to be discarded.

    I keep having trouble formatting it this way.
    Anyone have any ideas?
    All feedback is appreciated.

  2. #2
    Join Date
    Dec 2007
    Perhaps if you change the names of your variables to reflect what they actually point to it might become more obvious.

    pFirst = temp + 5;
    pLast = strchr(temp, ';');
    *pLast = '\0';

    pStartOfFullName = temp + 5;
    pEndOfFullName = strchr(temp, ';');
    *pEndOfFullName = '\0';
    and put a little more thought into it. I bet you can come up with a solution. I'm also betting this is an important part of your assignment; coming up with the algorithm to do this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string manipulation
    By mmukhame321 in forum C++ Programming
    Replies: 2
    Last Post: 10-12-2011, 06:55 AM
  2. need help with string manipulation.
    By newbc in forum C Programming
    Replies: 5
    Last Post: 02-08-2011, 04:36 PM
  3. string vector to string pointer manipulation
    By stanlvw in forum C++ Programming
    Replies: 11
    Last Post: 07-16-2008, 01:43 AM
  4. string manipulation
    By wart101 in forum C++ Programming
    Replies: 5
    Last Post: 12-11-2006, 11:51 PM
  5. A little help with string manipulation
    By InvertedSaint in forum C Programming
    Replies: 17
    Last Post: 11-05-2005, 09:14 AM

Tags for this Thread