C String Manipulation

This is a discussion on C String Manipulation within the C Programming forums, part of the General Programming Boards category; So I have a string containing student information which I've scanned in from a text file. Originally it looked something ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    46

    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:
    Code:
    char** buildList (int size)
    {
        char** list;
        char temp[MAX_SIZE];
        char* pFirst, *pLast;
    
        if(!(list = (char**)calloc (size + 1, sizeof(char*))))
            MEM_ERROR;
    
        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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Perhaps if you change the names of your variables to reflect what they actually point to it might become more obvious.

    Change
    Code:
    pFirst = temp + 5;
    pLast = strchr(temp, ';');
    
    *pLast = '\0';
    to

    Code:
    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, 03: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, 10:51 PM
  5. A little help with string manipulation
    By InvertedSaint in forum C Programming
    Replies: 17
    Last Post: 11-05-2005, 08:14 AM

Tags for this Thread


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