strpbrk problem

Could somebody please help me locate what went wrong with this program?

It's supposed to get a sentence from the user and display the number in that sentence in a new line.

For example.

"She baked 18 cookies." -->User-input
"18" -->Program output

Whenever I run this program, it would display not only the number but a whole bunch of symbols placed after the number.

Eh, what's wrong with what I wrote?

Please help, thanks.

:D

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 80

main()
{
char sen[max];
char num[max];
char buf[max];
char find[] = "0123456789";
char * kk;
int x, z, y;

fgets(str, max, stdin);

kk = strpbrk(str, find);
x=0;


do
{
buf[x]=*kk;
printf("%c\n", *kk);
kk = strpbrk(kk+1, find);
x++;
}
while(kk != NULL);

printf("\nThe extracted number is %s.\n", buf);

}

Oh, and it's also assuming that the sentence from the user will contain numbers. :D








Put after
 
while(kk != NULL);

this
 
buf[x]=0;

Since you weren't terminating the string, printf() was reading past the supposed end until it eventually found a 0.
And please use [code] tags in the future.
Oh! I get it.

Thank you very much!!!

Oops, sorry... I'm just new here...I'll keep that in mind for future posts. n_n

Thanks again!!!
Topic archived. No new replies allowed.