Searching a String for a Character - C String

C examples for String:Search Substring

Description

Searching a String for a Character

Demo Code

#include <stdio.h>
#include <string.h>
int main(void)
{
  char str1[] = "this is a test.";
  char str2[] = "test";
  char str3[] = "is";

  // Search str1 for the occurrence of str2
  if(strstr(str1, str2))
    printf("\"%s\" was found in \"%s\"\n",str2, str1);
  else//from   w  w  w  . j  a v  a 2 s.  c o  m
    printf("\n\"%s\" was not found.", str2);

  // Search str1 for the occurrence of str3
  if(!strstr(str1, str3))
    printf("\"%s\" was not found.\n", str3);
  else
    printf("\nWe shouldn't get to here!");
  return 0;
}

Result


Related Tutorials