String question

This is a discussion on String question within the C Programming forums, part of the General Programming Boards category; Hi, Again a question from Koffman's book; Question inputs a string and separate it into components. For example; Input : ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    48

    String question

    Hi,
    Again a question from Koffman's book;
    Question inputs a string and separate it into components.
    For example;
    Input :
    ALT1203S14
    Output:
    Warehouse: ALT
    Product: 1203
    Qualifiers: S14

    My broken code:
    Code:
    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
    	int first, next,n;
    	char code[40], warehouse[20], qualifiers[20], product[20];
    	printf("Enter product code> ");
    	scanf("%s", code);
    	first = 0;
    	next = 1; 
    	n = 0;
    	while(next<strlen(code)){
    		next++;
    		//n++;
    		if(code[next] >= 'A' && code[next] <= 'Z'){
    				n = n + 2;
    				strncpy(warehouse, code, next - n);
    				warehouse[next - n] = '\0';
    				first = next - n;
    				//next = next - n;
    		}
    		if(code[next] >='1' && code[next] <= '9'){
    			strncpy(product, &code[first], next - first );
    			product[next] = '\0';
    			first = next-n;
    		}
    		if(code[next] >= 'A' && code[next] <= 'Z'){			
    			strncpy(qualifiers, &code[first], next - first);
    			qualifiers[next-n] = '\0';
    			first = next;
    		}
    	}
    	printf("Warehouse: %s\nProduct: %s\nQualifiers: %s",warehouse,\
    product, qualifiers);
    	printf("\n%d\n%d", n,next);
    	return(0);
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Can you elaborate on why it is broken? Also you might want to take a look at strtok() for tokenizing strings.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Quote Originally Posted by claudiu View Post
    Can you elaborate on why it is broken? Also you might want to take a look at strtok() for tokenizing strings.
    First of all, it does not give proper output.
    İts output:
    Warehouse: ALT
    Product: 232S1
    Qualifiers: 123
    And I will look at strtok.But I guess the problem is in my if statements.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If the text format of each entry is always 3 letters, 4 numbers and 3mixed, the problem becomes very simple...
    Code:
    char buffer[12];
    
    char prefix[4];
    char digits[5];
    char extra[4];
    
    // get prefix
    strncpy(buffer,prefix,3);
    strncpy &buffer[3],digits,4);
    strncpy &buffer[7],extra,4);

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Quote Originally Posted by CommonTater View Post
    If the text format of each entry is always 3 letters, 4 numbers and 3mixed, the problem becomes very simple...
    Code:
    char buffer[12];
    
    char prefix[4];
    char digits[5];
    char extra[4];
    
    // get prefix
    strncpy(buffer,prefix,3);
    strncpy &buffer[3],digits,4);
    strncpy &buffer[7],extra,4);
    Thanks, I solved the problem.
    Code:
    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
    	int first, next,n, x;
    	char code[40], warehouse[20], qualifiers[20], product[20];
    	printf("Enter product code> ");
    	scanf("%s", code);
    	first = 0;
    	next = 1; 
    	n = 0;
    	while(next<strlen(code)){
    		next++;
    		if(code[next] >='1' && code[next] <= '9'){
    			strncpy(qualifiers, &code[first], next-1 );
    			qualifiers[next-1] = '\0';
    			first = next-1;		
    			}
    		else if(code[next] >= 'A' && code[next] <= 'Z'){
    			n = n + 2;
    			strncpy(warehouse, code, next - n);
    			warehouse[next - n] = '\0';
    			first = next - n;
    			x = n - 1;
    			strncpy(product, &code[first], next - x);
    			product[next-x] = '\0';
    		}
    	}
    	printf("Warehouse: %s\nProduct: %s\nQualifiers: %s",warehouse,\
    product, qualifiers);
    	return(0);
    }
    Enter product code> ATL1203S14
    Warehouse: ATL
    Product: 1203
    Qualifiers: S14

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by paranoidgnu View Post
    Thanks, I solved the problem.
    Code:
    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
    	int first, next,n, x;
    	char code[40], warehouse[20], qualifiers[20], product[20];
    	printf("Enter product code> ");
    	scanf("%s", code);
    	first = 0;
    	next = 1; 
    	n = 0;
    	while(next<strlen(code)){
    		next++;
    		if(code[next] >='1' && code[next] <= '9'){
    			strncpy(qualifiers, &code[first], next-1 );
    			qualifiers[next-1] = '\0';
    			first = next-1;		
    			}
    		else if(code[next] >= 'A' && code[next] <= 'Z'){
    			n = n + 2;
    			strncpy(warehouse, code, next - n);
    			warehouse[next - n] = '\0';
    			first = next - n;
    			x = n - 1;
    			strncpy(product, &code[first], next - x);
    			product[next-x] = '\0';
    		}
    	}
    	printf("Warehouse: %s\nProduct: %s\nQualifiers: %s",warehouse,\
    product, qualifiers);
    	return(0);
    }
    Very good... I was suggesting that you don't need to go at the string with loops if you know it is in a fixed format... you can just supply the offsets to strncpy and let it copy the field size you need.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string question
    By insanoflex in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2011, 09:50 AM
  2. string question.
    By MegaManZZ in forum C++ Programming
    Replies: 4
    Last Post: 02-19-2008, 11:13 AM
  3. A little string question
    By geek@02 in forum C++ Programming
    Replies: 1
    Last Post: 01-20-2005, 05:05 AM
  4. c++ string question
    By san in forum C++ Programming
    Replies: 10
    Last Post: 07-12-2002, 01:07 PM
  5. Another string question...
    By Unregistered in forum C Programming
    Replies: 18
    Last Post: 06-01-2002, 07:27 PM

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