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); } 

  1. main not have return-type void int.
  2. scanf can fail. check return-value.
    if successful, returns number of parameters assigned.
  3. %s reads non-whitespace, until next whitespace (thus 1 word).
  4. %s not limit how many non-whitespace characters read. buffer-overflow can deadly.
    use %71s (buffer-size: string-length + 1 terminator)
  5. you swapped arguments strstr.

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -