C语言教程
C语言控制语句
C语言函数
C语言数组
C语言指针
C语言字符串
C语言数学函数
C语言结构
C语言文件处理
C预处理器

C语言字符串函数

" string.h"库中定义了许多重要的字符串函数。
功能 说明
strlen(string_name) 返回字符串名称的长度。
strcpy(destination, source) 将源字符串的内容复制到目标字符串。
strcat(first_string, second_string) 用第一个字符串与第二个字符串连接或连接。字符串的结果存储在第一个字符串中。
strcmp(first_string,second_string) 比较第一个字符串和第二个字符串。如果两个字符串相同,则返回0。
strrev(string) 返回反向字符串。
strlwr(string) 以小写形式返回字符串字符。
strupr(string) 以大写字母返回字符串字符。

strlen()方法

#include
   
      
#include 
    
      int main(){ char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'}; printf("Length of string is: %d",strlen(ch)); return 0; } 
    
   
输出:
Length of string is: 10

strcpy()方法

#include
   
      
#include 
    
      int main(){ char ch[20]={'l', 'i', 'd', 'i', 'h', 'u', 'o', '\0'}; char ch2[20]; strcpy(ch2,ch); printf("Value of second string is: %s",ch2); return 0; } 
    
   
输出:
Value of second string is: lidihuo

strcat()方法

#include
   
      
#include 
    
      int main(){ char ch[10]={'h', 'e', 'l', 'l', 'o', '\0'}; char ch2[10]={'c', '\0'}; strcat(ch,ch2); printf("Value of first string is: %s",ch); return 0; } 
    
   
输出:
Value of first string is: helloc

strcat()方法

#include
   
      
#include 
    
      int main(){ char str1[20],str2[20]; printf("Enter 1st string: "); gets(str1);//reads string from console printf("Enter 2nd string: "); gets(str2); if(strcmp(str1,str2)==0) printf("Strings are equal"); else printf("Strings are not equal"); return 0; } 
    
   
输出:
Enter 1st string: hello
Enter 2nd string: hello
Strings are equal

strrev()方法

#include
   
      
#include 
    
      int main(){ char str[20]; printf("Enter string: "); gets(str);//reads string from console printf("String is: %s",str); printf("\nReverse String is: %s",strrev(str)); return 0; } 
    
   
输出:
Enter string: lidihuo
String is: lidihuo Reverse String is: ouhidil

strlwr()方法

#include
   
      
#include 
    
      int main(){ char str[20]; printf("Enter string: "); gets(str);//reads string from console printf("String is: %s",str); printf("\nLower String is: %s",strlwr(str)); return 0; } 
    
   
输出:
Enter string: LIdihuo
String is: LIdihuo
Lower String is: lidihuo

strupr()方法

#include
   
      
#include 
    
      int main(){ char str[20]; printf("Enter string: "); gets(str);//reads string from console printf("String is: %s",str); printf("\nUpper String is: %s",strupr(str)); return 0; } 
    
   
输出:
Enter string: lidihuo
String is: lidihuo
Upper String is: LIDIHUO
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4