文字列処理関数大全(四)

7415 ワード

strncmp
    :extern int strcmp(char *s1,char * s2,int n);
        
    :#include <string.h>
  
    :     s1 s2  n   。
  
    :
         s1<s2 ,   <0
         s1=s2 ,   =0
         s1>s2 ,   >0
  
    :

      // strncmp.c
      
      #include <syslib.h>
      #include <string.h>

      main()
      {
        char *s1="Hello, Programmers!";
        char *s2="Hello, programmers!";
        int r;
        
        clrscr();
        
        r=strncmp(s1,s2,6);
        if(!r)
          printf("s1 and s2 are identical");
        else
        if(r<0)
          printf("s1 less than s2");
        else
          printf("s1 greater than s2");
        
        getchar();
        clrscr();
        
        r=strncmp(s1,s2,10);
        if(!r)
          printf("s1 and s2 are identical");
        else
        if(r<0)
          printf("s1 less than s2");
        else
          printf("s1 greater than s2");

        getchar();
        return 0;
      }
      
      :bcmp,memcmp,stricmp,strnicmp


 
strnicmp,strncmpi
    :extern int strnicmp(char *s1,char * s2,int n);
        
    :#include <string.h>
  
    :     s1 s2  n          。
  
    :strncmpi  strnicmp    
         s1<s2 ,   <0
         s1=s2 ,   =0
         s1>s2 ,   >0
  
    :

      // strnicmp.c
      
      #include <syslib.h>
      #include <string.h>

      main()
      {
        char *s1="Hello, Programmers!";
        char *s2="Hello, programmers!";
        int r;
        
        clrscr();
        
        r=strnicmp(s1,s2,strlen(s1));
        if(!r)
          printf("s1 and s2 are identical");
        else
        if(r<0)
          printf("s1 less than s2");
        else
          printf("s1 greater than s2");
        
        getchar();
        return 0;
      }
      
      :bcmp,memcmp,stricmp,strncmp


strpbrk
    :extern char *strpbrk(char *s1, char *s2);
        
    :#include <string.h>
  
    :    s1      s2                   ,   NULL     。
  
    :    s1             ,              NULL。

  
    :


      // strpbrk.c
      
      #include <syslib.h>
      #include <string.h>

      main()
      {
        char *s1="Welcome To Beijing";
        char *s2="BIT";
        char *p;
        
        clrscr();
                
        p=strpbrk(s1,s2);
        if(p)
          printf("%s/n",p);
        else
          printf("Not Found!/n");
		
        p=strpbrk(s1, "Da");
        if(p)
          printf("%s",p);
        else
          printf("Not Found!");

        getchar();
        return 0;
      }
      
      :strcspn,strtok


strrev
    :extern char *strrev(char *s);
        
    :#include <string.h>
  
    :    s            (      NULL)。
  
    :               。

  
    :


      // strrev.c
      
      #include <syslib.h>
      #include <string.h>

      main()
      {
        char *s="Welcome To Beijing";
        
        clrscr();
        textmode(0x00);  // 6 lines per screen
        
        printf("%s/n%s",s,strrev(strdup(s)));
                

        getchar();
        return 0;
      }
      
      : 


strset
    :extern char *strset(char *s, char c);
        
    :#include <string.h>
  
    :    s            c。
  
    :    s   。
  
    :


      // strset.c
      
      #include <syslib.h>
      #include <string.h>

      main()
      {
        char *s="Golden Global View";
        
        clrscr();
        
        strset(s,'G');
        printf("%s",s);

        getchar();
        return 0;
      }
      
      :bzero,memset,setmem


strstr
    :extern char *strstr(char *haystack, char *needle);
        
    :#include <string.h>
  
    :    haystack   needle        (      NULL)。
  
    :         needle     ,        NULL。
  
    :


      // strstr.c
      
      #include <syslib.h>
      #include <string.h>

      main()
      {
        char *s="Golden Global View";
        char *l="lob";
        char *p;
        
        clrscr();
        
        p=strstr(s,l);
        if(p)
          printf("%s",p);
        else
          printf("Not Found!");

        getchar();
        return 0;
      }

strtok
    :extern char *strtok(char *s, char *delim);
        
    :#include <string.h>
  
    :           。s        ,delim       。
  
    :     ,s           ,      s  NULL。
        strtok s      delim      NULL('/0')   ,         。
                  。             NULL。
  
    :


      // strtok.c
      
      #include <syslib.h>
      #include <string.h>
      #include <stdio.h>

      main()
      {
        char *s="Golden Global View";
        char *d=" ";
        char *p;
        
        clrscr();
        
        p=strtok(s,d);
        while(p)
        {
          printf("%s/n",s);
          strtok(NULL,d);
        }

        getchar();
        return 0;
      }
      
      :strcspn,strpbrk


strupr
    :extern char *strupr(char *s);
        
    :#include <string.h>
  
    :    s       
  
    :   s        ,       。    s   。
  
    :


      // strupr.c
      
      #include <syslib.h>
      #include <string.h>

      main()
      {
        char *s="Copywrite 1999-2000 GGV Technologies";
        
        clrscr();
        
        printf("%s",strupr(s));

        getchar();
        return 0;
      }
      
      :strlwr