Thread: Help on extracting word from a string and store it on a int variable

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    Help on extracting word from a string and store it on a int variable

    Hi,

    I'm writing a program where user need to enter a command with some parameters. Something like, "please insert your choice:account change 123" where I need to get the "123" inside a integer variable.

    I've made some search but I was not able to find any answer. This is what I have so far:

    Code:
    	char s[50],ch;
    
    	int valor[10];
    
    	     int i,c=0,d=0;
    
    	     printf("Enter any string : ");
    	     for(i=0;ch!='\n';i++){
    	     ch=getchar();
    	     printf("-%c",ch);
    	     s[i]=ch;
    	     }
    	     s[i]='\0';
    
    	     for(i=0;s[i]!='\0';i++){
    	    if(s[i]==' '){
    
    	       c++;
    
    	    }
    	    if(c==2 && s[i]!=' '){
    	    	printf("%c",s[i]);
    	    	valor[d]=s[i];
    	    	d++;
    	    }
    	     }
    	     printf("\n\nTotal words are %d",c+1);
    	     printf("\n\n are %i", valor);
    Can someone help me on this?

    Thanks for your help.

    Regards
    SP

  2. #2
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    Any other solution to acheive the same objective is also welcome. Thanks again for your help.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Use fgets on stdin to read a line of input. Then use sscanf to parse out any strings or numbers you need.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    Thanks. That one made the job!

    Here is the code to help anyone out there:

    Code:
    char temp[50],tipo1[20], tipo2[20];
    	int v1;
    
    	printf("operacao:");
    	fgets(&temp,255,stdin);
    
    	printf("\n%s", &temp);
    
    	sscanf(temp,"%s %s %i",&tipo1,&tipo2,&v1);
    
    	printf("\n%s",&tipo1);
    	printf("\n%s",&tipo2);
    	printf("\n%i",v1);

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    	printf("operacao:");
    	fgets(&temp,255,stdin);        // drop &
     
    	printf("\n%s", &temp);            // drop &
    
    	sscanf(temp,"%s %s %i",&tipo1,&tipo2,&v1);  // & not needed except v1
    
    	printf("\n%s",&tipo1);        // drop &
    	printf("\n%s",&tipo2);        // drop &
    	printf("\n%i",v1);
    That should *NOT* work. Question 12.12b

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    31

    look at the strtok library call.

    man strtok

    STRTOK(3) NEWLIB

    NAME
    6.37 `strtok', `strtok_r', `strsep'--get next token from a string

    SYNOPSIS
    #include <string.h>
    char *strtok(char *SOURCE, const char *DELIMITERS)
    char *strtok_r(char *SOURCE, const char *DELIMITERS,
    char **LASTS)
    char *strsep(char **SOURCE_PTR, const char *DELIMITERS)

    DESCRIPTION
    The `strtok' function is used to isolate sequential tokens in a null-terminated string

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. Memory leak - need help finding
    By ChadJohnson in forum C++ Programming
    Replies: 8
    Last Post: 04-06-2005, 07:26 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM

Tags for this Thread