Хелпикс

Главная

Контакты

Случайная статья





Обработка строк



Обработка строк

Для выполнения описанных в этом подразделе функций необходимо включить в программу файл string.h командой

#include <string.h>

strcat - сцепить две строки.

Определение: char *strcat(s1,s2)

char *s1, *s2;

Пример 1:

/* сцепить две строки */

/* в головном файле conio.h содержится функция очистки экрана clrscr( ) */

#include <string.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{ clrscr();

   char destination[25];

   char *blank = " ", *c = "C++", *turbo = "Turbo";

   strcpy(destination, turbo);

   strcat(destination, blank);

   strcat(destination, c);

   printf("%s\n", destination);

   getch();

   return 0;

}

strncat - сцепить две строки, причем из второй строки копировать не более n символов.

Определение: char *strncat(s1,s2,n)

char *s1, *s2;

int n;

Пример 2:

/* cцепить две строки, причем из второй строки

копировать не более n символов */

#include <string.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

clrscr();

   char destination[25];

   char *source = "structured ";

   strcpy(destination, "programming");

   strncat(destination, source, 11);

   printf("%s\n", destination);

   getch();

   return 0;

}

strcmp - сравнить две строки в лексикографическом порядке.

Определение: int strcmp(s1,s2)

char *s1, *s2;

Пример 3:

#include <string.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

   char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";

   int ptr;

   clrscr();

   ptr = strcmp(buf2, buf1);

   if (ptr > 0)

          printf("buffer 2 is greater than buffer 1\n");

   else

          printf("buffer 2 is less than buffer 1\n");

   ptr = strcmp(buf2, buf3);

   if (ptr > 0)

          printf("buffer 2 is greater than buffer 3\n");

   else

          printf("buffer 2 is less than buffer 3\n");

   getch();

   return 0;

}

strncmp - сравнить первые n символов двух строк.

Определение: int strncmp(s1,s2, n)

char *s1, *s2;

int n;

Пример 4:

#include <string.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

   char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc";

   int ptr;

   clrscr();

   ptr = strncmp(buf2,buf1,3);

   if (ptr > 0)

          printf("buffer 2 is greater than buffer 1\n");

   else

          printf("buffer 2 is less than buffer 1\n");

 

   ptr = strncmp(buf2,buf3,3);

   if (ptr > 0)

          printf("buffer 2 is greater than buffer 3\n");

   else

          printf("buffer 2 is less than buffer 3\n");

   getch();

   return(0);

}

strcpy - копировать строку s2 в строку s1.

Определение: char *strcpy(s1,s2)

char *s1, *s2;

Пример 5:

#include <stdio.h>

#include <string.h>

#include <conio.h>

int main(void)

{

clrscr();

   char string[10];

   char *str1 = "abcdefghi";

 

   strcpy(string, str1);

   printf("%s\n", string);

   getch();

   return 0;

}

strncpy - копировать не более n символов строки s2.

Определение: char *strncpy(s1,s2,n)

char *s1, *s2;

int n;

Пример 6:

#include <stdio.h>

#include <string.h>

#include <conio.h>

int main(void)

{

   clrscr();

   char string[10];

   char *str1 = "abcdefghi";

   strncpy(string, str1, 3);

   string[3] = '\0';

   printf("%s\n", string);

   getch();

   return 0;

}

strlen - определить длину строки (число символов без завершающего нулевого символа).

Определение: int strlen(s)

char *s;

Пример 7:

#include <stdio.h>

#include <string.h>

#include <conio.h>

int main(void)

{

   clrscr();

   char *string = "Borland International";

   printf("%d\n", strlen(string));

   getch();

   return 0;

}

strchr - найти в строке первое вхождение символа n.

Определение:

char *strchr(s,n)

char *s;

int n;

Пример 8:

#include <string.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

clrscr();

   char string[20];

   char *ptr, c = 'r';

   strcpy(string, "This is a string");

   ptr = strchr(string, c);

   if (ptr)

    printf("The character %c is at position: %d\n", c, ptr);

   else

    printf("The character was not found\n");

    getch();

   return 0;

}

strrchr - найти в строке последнее вхождение символа с.

Определение:

char *strrchr(s,c)

char *s;

int c;

Пример 9:

#include <string.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

clrscr();

   char string[20];

   char *ptr, c = 'r';

   strcpy(string, "This is a string");

   ptr = strrchr(string, c);

   if (ptr)

          printf("The character %c is at position: %d\n", c, *ptr);

   else

          printf("The character was not found\n");

          getch();

   return 0;

}

strpbrk - найти в строке s1 любой из множества символов, входящих в строку s2.

Определение:

char *strpbrk(s1,s2)

char *s1, *s2;

Пример 10:

#include <stdio.h>

#include <string.h>

#include <conio.h>

int main(void)

{

   clrscr();

   char *string1 = "abcdefghijklmnopqrstuvwxyz";

   char *string2 = "onm";

   int *ptr;

   ptr = strpbrk(string1, string2);

   if (ptr)

          printf("strpbrk found first character: %c\n", *ptr);

   else

          printf("strpbrk didn't find character in set\n");

   getch();

return 0;

}

strspn - определить длину отрезка строки s1, содержащего символы из множества, входящих в строку s2.

Определение:

int strspn(s1,s2)

char *s1, *s2;

Пример 11:

#include <stdio.h>

#include <string.h>

#include <alloc.h>

#include <conio.h>

int main(void)

{

   clrscr();

   char *string1 = "1234567890";

   char *string2 = "123DC8";

   int length;

   length = strspn(string1, string2);

   printf("Character where strings differ is at position %d\n", length);

   getch();

   return 0;

}

strcspn - определить длину отрезка строки s1, не содержащего символы cтроки s2.

Определение:

int strcspn(s1,s2)

char *s1, *s2;

Пример 12:

#include <stdio.h>

#include <string.h>

#include <alloc.h>

#include <conio.h>

int main(void)

{

   clrscr();

   char *string1 = "1234567890";

   char *string2 = "747DC8";

   int length;

   length = strcspn(string1, string2);

   printf("Character where strings intersect is at position %d\n", length);

   getch();

   return 0;

}

strtok - выделить из строки s1 лексемы, разделенные любым из множества символов, входящих в строку s2.

Определение: char *strtok(s1,s2)

char *s1, *s2;

Пример 13:

#include <string.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

clrscr();

   char input[16] = "abc,d";

   char *p;

   p = strtok(input, ",");

   if (p) printf("%s\n", p);

   p = strtok(NULL, ",");

   if (p) printf("%s\n", p);

   getch();

   return 0;

}

 



  

© helpiks.su При использовании или копировании материалов прямая ссылка на сайт обязательна.