C Tutorial/string.h/strrchr — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
(нет различий)

Текущая версия на 10:32, 25 мая 2010

strrchr

Item Value Header file string.h Declaration char *strrchr(const char *str, int ch); Return returns a pointer to the last occurrence of ch in *str.

If no match is found, a null pointer is returned.


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

Using strchr

#include <stdio.h>
#include <string.h>
int main()
{
   const char *string = "This is a test";
   char character1 = "a";
   char character2 = "z";
   
   if ( strchr( string, character1 ) != NULL ) {
      printf( "\"%c\" was found in \"%s\".\n", 
         character1, string );
   } else { 
      printf( "\"%c\" was not found in \"%s\".\n", 
         character1, string );
   }
   if ( strchr( string, character2 ) != NULL ) {
      printf( "\"%c\" was found in \"%s\".\n", 
         character2, string );
   } else { 
      printf( "\"%c\" was not found in \"%s\".\n", 
         character2, string );
   } 
   return 0;
}
"a" was found in "This is a test".
"z" was not found in "This is a test".