Help parsing

This is a discussion on Help parsing within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> char *trim(char *); int main(void) { int i, j, length; char name[20]; char actors[] = ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Help parsing

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *trim(char *);
    
    int main(void)
    {
    	int i, j, length;
    	char name[20];
    	char actors[] = "Kate Winslet, Johnny Depp, Morgan Freeman, Tom Hanks";
    
    	j = 0;
    	length = strlen(actors);
    
    	for(i = 0; i < length; i++){
    		
    		if(actors[i] != ','){
    			name[j] = actors[i];
    			j++;
    		}
    
    		else{
    			name[j] = '\0';
    			printf("%s\n", trim(name));
    			j = 0;
    		}
    	}
    
    	return 0;
    }
    
    char *trim(char *array)
    {
        int i;
        
        for(; *array == ' '; array++);
        
        i = strlen(array);
        
        while((--i > 0) && (array[i] == ' '))
            array[i] = 0;
        
        return array;
    }
    This code won't display Tom Hank's name. How can i fix it? Thank you.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    For this you could use strtok like this. I didn't compile and test it but it should give you an idea.
    Code:
    char *ptr
    ptr=strtok(Actors, " ,\n");
    while (ptr!=NULL)
    {
    	while(*ptr==' ')ptr++;
    	printf("%s\n", ptr);
    	ptr=strtok(NULL, ",\n");
    }
    Edited to fix bug
    Last edited by Quantum1024; 04-02-2005 at 07:56 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    It worked. Thank you.
    I have one question though: why did you use NULL instead of actors when you called strtok inside the while loop?

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    char actors[] = "Kate Winslet, Johnny Depp, Morgan Freeman, Tom Hanks,";

    Tell me what I changed in that string - which caused your program to print "Tom Hanks"

    xeddiex.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    man strtok
    caduardo21
    When calling strtok the second time I use NULL to specify that I'm still working on the same sting, strtok keeps a static variable with the curent potision in the string.
    Last edited by Quantum1024; 04-02-2005 at 08:03 PM.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by caduardo21
    This code won't display Tom Hank's name. How can i fix it? Thank you.

    Your loop stops after the last char in the string (not the terminating '\0', but the 's' in Hanks). So the last name in the string is not printed.

    so you could change the loop expression to this:

    Code:
    for(i = 0; i <= length; i++){
    Now, in order to recognize the end of the last name in the string, it's not a comma you are looking for, but a '\0'. You could try this so that the else part gets to terminate and print the last name:

    Code:
    	if((actors[i] != ',' ) && (actors[i] != '\0')){
    			name[j] = actors[i];
    	else {
    Regards,

    Dave

  7. #7
    Registered User coolshyam's Avatar
    Join Date
    Mar 2005
    Posts
    26

    Cool small mistake

    the array is returned only after you identify the space bar next to a name . so, modify the progam in the following line
    Code:
    char actors[] = "Kate Winslet, Johnny Depp, Morgan Freeman, Tom Hanks, ";

    any questions any type in programming

    a ready made answer
    -----------------------------------------------------------------------------------
    FORTUNE FAVOURS THE BOLD!
    -----------------------------------------------------------------------------------

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes, let's not fix the function so it works and is better. Let's instead change the input to it to fit the flawed function.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need sth about parsing
    By Masterx in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2008, 11:55 PM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. draw tree graph of yacc parsing
    By talz13 in forum C Programming
    Replies: 2
    Last Post: 07-23-2006, 01:33 AM
  4. Parsing for Dummies
    By MisterWonderful in forum C++ Programming
    Replies: 4
    Last Post: 03-08-2004, 04:31 PM
  5. I hate string parsing with a passion
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-19-2002, 06:30 PM

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