C/String/String Search

Материал из C\C++ эксперт
Перейти к: навигация, поиск

A demonstration of seeking and finding

#include <stdio.h>
#include <string.h>
void main()
{
  char str1[] = "This string contains the key.";
  char str2[] = "the key";
  char str3[] = "the keys";
  if(strstr(str1, str2) == NULL)
    printf("\nString not found.");
  else
    printf("\nString: %s\n was found in string: %s",str2, str1);
  if(strstr(str1, str3) == NULL)
    printf("\nString not found.");
  else
    printf("\nWe shouldn"t get to here!");
}


Find character in string: how to use strchr

#include <stdio.h>
#include <string.h>
int main ()
{
  char str[] = "This is a line";
  char *p;
  
  printf ("Looking for "s" character in \"%s\"...\n", str);
  
  p = strchr(str, "s");
  
  while (p != NULL) {
    printf ("found at %d\n",p - str + 1);
    p = strchr(p + 1, "s");
  }
  return 0;
}


Finding occurrences of one string in another: strstr

#include <stdio.h>
#include <string.h>
#include <ctype.h>
void main() {
  char text[100];             
  char substring[40];         
  int i = 0;                  
  
  printf("\n string to be searched(less than 100 characters):\n");
  gets(text);
  
  printf("\n string sought (less than 40 characters ):\n");
  gets(substring);
 
  /* Convert both strings to upper case. */
  for(i = 0 ; (text[i] = toupper(text[i])) != "\0" ; i++);
  for(i = 0 ; (substring[i] = toupper(substring[i])) != "\0" ; i++);
    printf("\nThe second string %s found in the first.\n",
              ((strstr(text, substring) == NULL) ? "was not" : "was"));
}


Find last occurrence of character in string

#include <string.h>
#include <stdio.h>
int main(void)
{
  char *p;
  p = strrchr("this is a test", "i");
  printf(p);
  return 0;
}


Find last occurrence of character in string: how to use strrchr

#include <stdio.h>
#include <string.h>
int main ()
{
  char str[] = "This is a line";
  char *p;
  
  p = strrchr(str, "i");
  printf ("Last occurence of "i" is %d \n", p - str + 1);
  
  return 0;
}


Find substring

#include <string.h>
#include <stdio.h>
int main(void)
{
  char *p;
  p = strstr("this is a test", "is");
  printf(p);
  return 0;
}


Find substring: how to use strstr

#include <stdio.h>
#include <string.h>
int main ()
{
  char str[] ="This is a line";
  char *p;
  
  p = strstr (str, "is");
  strncpy (p, "sample",5 );
  puts (str);
  
  return 0;
}


Find sub string: our own function

#include <stdio.h>
int find_substr(char *listPointer, char *itemPointer);
int main(void)
{
  if(find_substr("C is fun", "is") != -1)
    printf("Substring is found.");
  return 0;
}
/* Return index of first match of itemPointer in listPointer. */
int find_substr(char *listPointer, char *itemPointer)
{
  int t;
  char *p, *p2;
  for(t=0; listPointer[t]; t++) {
    p = &listPointer[t];
    p2 = itemPointer;
    while(*p2 && *p2==*p) {
      p++;
      p2++;
    }
    if(!*p2) return t; /* 1st return */
  }
   return -1; /* 2nd return */
}


Look up names in a hardcoded list

#define STRING_LENGTH 80
#include <stdio.h>
#include <string.h>
int lookup(char const *const name); /* lookup a name */
int main()
{
    char name[STRING_LENGTH] = "Jim";
    name[strlen(name)] = "\0";
    if (lookup(name)){
        printf("%s is in the list\n", name);
    }else{
        printf("%s is not in the list\n", name);
    }
    return (0);
}
int lookup(char const *const name)
{
    static char *list[] = {"John","Jim","Jane","Clyde",NULL};
    int index;
    for (index = 0; list[index] != NULL; ++index) {
        if (strcmp(list[index], name) == 0)
            return (1);
    }
    return (0);
}


Scan string for specified characters

#include <stdio.h>
#include <string.h>
int main(void)
{
  char *p;
  p = strpbrk("this is a test", " absj");
  printf(p);
  return 0;
}


Scan string for specified characters: how to use strpbrk

#include <stdio.h>
#include <string.h>
int main ()
{
  char str[] = "This is a line";
  char key[] = "aeiou";
  char *p;
  printf ("Vowels in "%s": ",str);
  p = strpbrk (str, key);
  while (p != NULL) {
  
    printf ("%c " , *p);
    p = strpbrk (p + 1, key);
  
  }
  printf ("\n");
  
  return 0;
}


Search a char inside a string

#include <stdio.h>
#include <string.h>
int main(void)
{
  char *p;
  p = strchr("this is a test", " ");
  printf(p);
  return 0;
}


Search string

#include <stdio.h>
#include <string.h>
int search(char *p[], char *name);
char *names[] = {"John","Jim","Jane","James",NULL};
int main(void)
{
  if(search(names, "James") != -1)
    printf("in list.\n");
  if(search(names, "Bill") == -1)
    printf("not found.\n");
  return 0;
}
int search(char *p[], char *name)
{
  int t;
  for(t=0; p[t]; ++t){
    if(!strcmp(p[t], name)) {
       return t;
    }
  }
    return -1; /* not found */
}


Search string for occurrence of character set

#include <string.h>
#include <stdio.h>
int main(void)
{
  int len;
  len = strcspn("this is a test", "ab");
  printf("%d", len);
  return 0;
}


Search string for occurrence of character set: how to use strcspn

#include <stdio.h>
#include <string.h>
int main ()
{
  char str1[] = "1245789000";
  char str2[] = "1234567890";
  int i;
  i = strcspn (str1,str2);
  printf ("The first number in str1 is str1[%d]\n", i);
  return 0;
}