Thread: string problem

  1. #1
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90

    string problem

    i got a string that looks lie hello(you) which is '\0' terminated.

    now i want to extract the words (means splitting it at ( and ).
    i wrote the following:

    Code:
    while(word[i] != '\0') {
    				while(word[i] != '(' && word[i] != ')' && word[i] != '\0')
    					++i;
    				if(word[i] == '(' || word[i] == ')') {
    					word[i] = '\0';
    					printf("%s | ",word[j]);
    					j = ++i;
    				}
    it crashes. any idea?

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    printf("%s | ",word[j]);

    Change to:

    printf("%s | ",&word[j]);

  3. #3
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90
    ou, that hurts.

    thanks

  4. #4
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    try this one...
    Code:
    int extract(char *src,char *dest,int dest_sz) {
    	char *psrc =src;
    	char *pdst = dest;	
    	while(*psrc && *psrc!='(') {
    		psrc++;
    	}
    	if(!*psrc) return -1; /*ERROR! '(' not found*/
    	psrc++;
    	while(*psrc && *psrc!=')') {
    		if(pdst - dest >=dest_sz)
    			break;/*dest is out of memory*/
    		*pdst = *psrc;
    		psrc++; 
    		pdst++;
    		
    	}
    	*pdst = '\0';
    
    	return pdst - dest; /*return the strlen of the dest*/
    }
    and this below is upgraded version
    with different delimiters:
    Code:
    int extract(char *src,char *dest,char dlm[3],int dest_sz) {
    	char *psrc =src;
    	char *pdst = dest;	
    	while(*psrc && *psrc!=dlm[0]) {
    		psrc++;
    	}
    	if(!*psrc) return -1; /*ERROR! '(' not found*/
    	psrc++;
    	while(*psrc && *psrc!=dlm[1]) {
    		if(pdst - dest >=dest_sz)
    			break;/*dest is out of memory*/
    		*pdst = *psrc;
    		psrc++; 
    		pdst++;
    		
    	}
    	*pdst = '\0';
    
    	return pdst - dest; /*return the strlen of the dest*/
    }

    Code:
    /*TEST*/
    int main(void) {
       char str[] = "This is a (test) {string}";
       char dest[200];
       int len=0;
       len = extract(src,dest,sizeof(dest));
       printf("\"%s\" -> \"%s\"\n",str,dest);
    
       len = extract(src,dest,"{}",sizeof(dest));
       printf("\"%s\" -> \"%s\"\n",str,dest);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM