C/String/String Compare

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

Compare some characters of two strings: how to use strncmp

<source lang="cpp">

  1. include <stdio.h>
  2. include <string.h>

int main () {

 char str[][5] = { "CS115" , "CS334" , "CS445" , "CS335" , "CS889" };
 int n;
 printf ("Looking for a class...\n");
 for (n=0 ; n < 5 ; n++)
   if (strncmp (str[n],"CS33*",1) == 0)
   {
     printf ("found %s\n",str[n]);
   }
 return 0;

}

      </source>


Compare strings: strcmp

<source lang="cpp">

  1. include <stdio.h>
  2. include <string.h>

int main() {

 char word1[20];
 char word2[20];
 printf("\n first word:\n1: ");
 scanf("%s", word1);                                 /* Read the first word    */
 printf(" second word:\n 2: ");
 scanf("%s", word2);                                 /* Read the second word   */
 /* Compare the two words */
 if(strcmp(word1,word2) == 0)
   printf("identical words");
 else
   printf("%s comes before %s", (strcmp(word1, word2) > 0) ? word2 : word1, (strcmp(word1, word2) < 0) ? word2 : word1);

}


      </source>


Compare two strings: how to use strcmp

<source lang="cpp">

  1. include <stdio.h>
  2. include <string.h>

int main () {

 char szKey[] = "Apple";
 char szInput[80];
 
 do {
 
    printf ("Which is my favourite computer? ");
    gets (szInput);
 
 } while (strcmp (szKey,szInput) != 0);
 
 printf ("Correct answer!\n");
 return 0;

}

      </source>


Our own string compare function

<source lang="cpp">

  1. include <stdlib.h>
  2. include <ctype.h>
  3. include <stdio.h>

int comp(const void *ch, const void *s); int main(void) {

 char *alpha = "abcdefghijklmnopqrstuvwxyz";
 char ch;
 char *p;
 printf("Enter a character: ");
 ch = getchar();
 ch = tolower(ch);
 p = (char *) bsearch(&ch, alpha, 26, 1, comp);
 if(p)
    printf(" %c is in alphabet\n", *p);
 else
    printf("is not in alphabet\n");
 return 0;

} /* Compare two characters. */ int comp(const void *ch, const void *s) {

 return *(char *)ch - *(char *)s;

}

      </source>


String compare: how to use strncmp

<source lang="cpp">

  1. include <stdio.h>
  2. include <string.h>
  3. include <stdlib.h>

int main(int argc, char *argv[]) {

 if(argc!=3) {
   printf("Incorrect number of arguments.");
   exit(1);
 }
 if(!strncmp(argv[1], argv[2], 8))
   printf("The strings are the same.\n");
 return 0;

}

      </source>


Testing characters in a string: is digit and is alpha

<source lang="cpp">

  1. include <stdio.h>
  2. include <ctype.h>

void main() {

 char buffer[80];               
 int i = 0;                     
 int num_letters = 0;           
 int num_digits = 0;            
 printf("\n string with char and digits:\n");
 gets(buffer);   /* Read a string into buffer  */
 while(buffer[i] != "\0") {
   if(isalpha(buffer[i]))
     num_letters++;             /* Increment letter count     */
   if(isdigit(buffer[i++]))
     num_digits++;              /* Increment digit count      */
 }
 printf("\n The string contained %d letters and %d digits.\n",
                                             num_letters, num_digits);

}


      </source>