用合适的 C 语言示例解释字符串库函数


字符串库函数

string.h 库 中提供了用于处理字符串的预定义函数。它们是:

strlen () 函数

它返回字符串中的字符数。

语法

int strlen (string name)

示例

#include <string.h>
main (){
   char a[30] = Hello”;
   int l;
   l = strlen (a);
   printf (“length of the string = %d”, l);
   getch ();
}

输出

length of the string = 5

strcpy () 函数

  • 它用于将源字符串复制到目标字符串。
  • 目标字符串的长度 >= 源字符串。

语法

strcpy (Destination string, Source String);

例如,

1) char a[50];
strcpy (“Hello”,a);
o/p: error
2) char a[50];
strcpy ( a,”hello”);
o/p: a= “Hello”

示例

#include <string.h>
main (){
   char a[50], b[50];
   printf ("enter a source string");
   scanf("%s", a);
   printf("enter destination string");
   scanf("%s",b);
   strcpy ( b,a);
   printf ("copied string = %s",b);
   getch ();
}

输出

Enter a source string : Hello
Copied string = Hello

strncpy () 函数

  • 它将源字符串的 'n' 个字符复制到目标字符串。

  • 目标字符串的长度必须 >= 源字符串。

语法

strncpy (Destination string, Source String, n);

示例

#include<string.h>
main (){
   char a[50], b[50];
   printf ("enter a string");
   gets (a);
   gets(b);
   strncpy (b,a,3);// copy first 3 char from a string
   b[3] = '\0';
   printf ("copied string = %s",b);
   getch ();
}

输出

Enter a string : Hello
Copied string = Hel
It is also used for extracting substrings;

strcat () 函数

  • 它组合两个字符串。
  • 目标字符串的长度必须 > 源字符串。

语法

strcat (Destination String, Source string);

示例

#include <string.h>
main(){
   char a[50] = "Hello";
   char b[20] = "Good Morning";
   clrscr ();
   strcat (a,b);
   printf("concatenated string = %s", a);
   getch ();
}

输出

Concatenated string = Hello Good Morning

strncat () 函数

  • 此函数用于将一个字符串的 n 个字符连接到另一个字符串。

  • 目标字符串的长度必须大于源字符串

  • 结果连接后的字符串将存储在目标字符串中。

语法

strncat (Destination String, Source string,n);

示例

#include <string.h>
main (){
   char a [30] = "Hello";
   char b [20] = "Good Morning";
   clrscr ();
   strncat (a,b,4);
   a [9] = '\0';
   printf("concatenated string = %s", a);
   getch ();
}

输出

Concatenated string = Hello Good.

strcmp() 函数(字符串比较)

  • 此函数比较两个字符串。

  • 它返回两个字符串中前两个不匹配字符的 ASCII 差值。

语法

int strcmp (string1, string2);
//If the difference is equal to zero, then string1 = string2
//If the difference is positive, then string1 > string2
//If the difference is negative, then string1 < string2

示例

#include<stdio.h>
#include<string.h>
int main (){
   char a[50], b [50];
   int d;
   printf ("Enter 2 strings:");
   scanf ("%s %s", a,b);
   d = strcmp(a,b);
   if (d==0){
      printf("%s is (alphabetically) equal to %s", a,b);
   }else if (d>0){
      printf("%s is (alphabetically) greater than %s",a,b);
   }else if (d<0){
      printf("%s is (alphabetically) less than %s", a,b);
   }
}

输出

Enter 2 strings:apple ball
apple is (alphabetically) less than ball

strncmp () 函数

此函数用于比较两个字符串的前 'n' 个字符。

语法

strncmp ( string1, string2,2)

例如,char a[10] = “the”;

       char b[10] = “there”

       strncmp (a,b,4);

       输出 - 两个字符串相等

strrev() 函数

  • 该函数用于反转字符串。
  • 反转后的字符串将存储在同一个字符串中。

语法

strrev (string)

示例

#include<stdio.h>
main (){
   char a[50] ;
   clrscr();
   printf ("enter a string");
   gets (a);
   strrev (a);
   printf("reversed string = %s",a)
   getch ();
}

输出

enter a string Hello
reversed string = olleH

strstr() 函数

  • 它用于搜索子字符串是否在主字符串中。

  • 它返回 s1 中 s2 第一次出现的指针。

语法

strstr(mainsring,substring);

示例

#include<stdio.h>
void main(){
   char a[30],b[30];
   char *found;
   printf("Enter a string:\t");
   gets(a);
   printf("Enter the string to be searched for:\t");
   gets(b);
   found=strstr(a,b);
   if(found)
      printf("%s is found in %s in %d position",b,a,found-a);
   else
      printf("-1 since the string is not found");
   getch();
}

输出

Enter a string: how are you
Enter the string to be searched for: you
you is found in 8 position

更新于: 2023年9月14日

34K+ 浏览量

启动您的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.