Please help me:
Following is my program:

main()
{
  char str[] = "This is //20";
  char c[10], *k;
  int num=0;

  sscanf(str, "%s%[^//]*//%d",c, &num);
  printf("%s \n %d \n", c, num);
}

My intention is to get the first string and last number. I want to ignore the remaining text. There is chance of having the number in middle of the string also, but the number starts with //.

Following are the possible strings that I can use as input strings:
This is //20
This is //kandarpa //20
This is //20 //kandarpa
This is

The string consists at most one number only.

sscanf is not up to dealling with all those different formats. You would be better off searching the string for // and then checking to see if it is followed by a digit and then if it is using atoi or strtol to convert that to a binary value.

Banfa told correctly, its far better to search for the occurance of // in the string.
Here's a program for u...
Note that I have considered the conditions and possible inputs given by u.
That is I haven't considered inputs like "This is /my number //34" (Single "/" occurance) etc.

I have tried to make it as simple as possible.

#include <stdio.h>

int main(void)
{
        int i = 0, j = 0, count = 0;
        char ch, str[50], num[10];

        printf("\nEnter a string : ");
        gets(str);      /* Take string input fron user with spaces */
        do
        {
                ch = str[i]; /* Storing each character of the string */

                if(count == 2)  /* That means already got "//" in the string */
                {
                        if((ch >= '0') && (ch <= '9'))  /* Its a digit */
                        {
                                num[j] = ch;    /* Store the number */
                                j++;
                        }
                        else
                                count = 0;      /* So that's a // not followed by a number, so need to search more */
                }

                if((ch == ' ') && (j != 0))     /* End of a word and already got a number in store */
                {
                        printf("\nThe number is : %s\n", num);
                        break;  /* Breaking the loop as only one number is acceptable in the string */
                }

                if(ch == '/')
                        count++;        /* Count number of occurances of "/" */

                i++;
        }while(ch != '\0'); /* Until end of the string */

        if(j == 0)
                printf("\nSorry no numbers in the string !!!\n"); /* Just in case we have got no numbers */

        return 0;
}

Check it and tell me whether it's fine for u...

Bye

Thanks...
Its really helpful

I have a similar problem. I would like to break up a string into a character array and a number but am not sure if sscanf can help me.

#include <stdio.h>

int main(void)
{

    char puppy[10]="DOG|1.2";
    char p[10];
    float num;

    sscanf(puppy,"%[^|]%f", &p, &num);
    printf("%s %f", p, num);

}

the output is DOG 0.000000

I would like to be able to store the number 1.2 in num but this doesn't work. As a further complication, the character array (e.g. DOG) can be any length.

If anyone could help it would be greatly appreciated!
Thanks!

@hanananah also will work for original poster
you guys better use this one strtok

or if you know pointers good, you can locate it '|' with strchr if you have only one of these, if you have more you can ofcourse make it work too.

Or do it manually, going through array and searching position of '|' then taking whats after before | or 0

sscanf(puppy,"%[^|]%f", &p, &num);

Try this instead:

sscanf(puppy,"%[^|]|%f", p, &num);

You forgot to extract the pipe character. Not to mention that the address-of operator is unneeded on p because it's already a pointer to char.

commented: Thank you! +0
commented: nice one, always happy to learn from u +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.