Common string manipulation functions for C language (i)

Source: Internet
Author: User
Tags strcmp

Always do is the microcontroller-related program design, so the program is more inclined to the bottom, the operation of the string is limited to the display of the LCD screen, etc., want to improve the level of the string operation, rather than clumsy array substitution, and so on, look at the post found that the C language string operation function is so rich and practical, This record is ready for use.

the strlen (): string length calculation function  

Application Examples:

1#include <stdio.h>2#include <string.h>3 4 CharTextbuff[] ="hello_my_friend!";5 6 intMainvoid)7 {8printf"the length of the Textbuff is:%d\r\n", strlen (Textbuff)); 9}

No.2 strcpy (): string copy function  

Application Examples:

1#include <stdio.h>2#include <string.h>3 4 Char*textbuff="hello_my_friend!";5 Charrevbuff[ -];6 7 intMainvoid)8 {9 strcpy (revbuff,textbuff);Tenprintf"revbuff:%s\r\n", Revbuff);  One}

No.3 strcat (): string concatenation function  

Application Examples:

1#include <stdio.h>2#include <string.h>3 4 intMainvoid)5 {6     Char*Textbuff;7     Char*a="IamA";8     Char*b="iamb";9     Char*c="IAMC"; Ten strcat (textbuff,a); One strcat (textbuff,b); A strcat (TEXTBUFF,C);  -printf"the length of the Textbuff is:%d\n", strlen (Textbuff)); -printf"%s\n", Textbuff); the}

No.4 STRCHR (): String lookup (first occurrence position)

Application Examples:

1#include <stdio.h>2#include <string.h>3 4 intMainvoid)5 {6     Chartext[Ten]="Wearetheab";7     Char*Ptr;8     CharA='a';9     TenPtr=STRCHR (text,a); Oneprintf"the position of a is in the first%d position of text \ n", ptr-text+1); A}

No.5 strcmp (): string comparison function

Application Examples:

1#include <stdio.h>2#include <string.h>3 4 intMainvoid)5 {6     Char*a="hello!";7     Char*b="hello!";8     Charnum=0;9num=strcmp (A, b);Ten     if(num==0) One     { Aprintf"two arrays equal \ n"); -     } -     Else the     { -printf"two arrays not equal \ n"); -     } -}

Common string manipulation functions for C language (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.