Using strstr to determine if a given string contains a string with spaces [C] -
i'm working through example of using strstr()
function.
if input "pamela sue smith", why program output ""pamela" sub-string!" , not ""pamela sue smith" sub-string!".
#include <stdio.h> #include <string.h> void main(void) { char str[72]; char target[] = "pamela sue smith"; printf("enter string: "); scanf("%s", str); if (strstr( target, str) != null ) printf(" %s sub-string!\n", str); }
main
not have return-typevoid
int
.scanf
can fail. check return-value.
if successful, returns number of parameters assigned.%s
reads non-whitespace, until next whitespace (thus 1 word).%s
not limit how many non-whitespace characters read. buffer-overflow can deadly.
use%71s
(buffer-size: string-length + 1 terminator)- you swapped arguments
strstr
.
Comments
Post a Comment