Linux C Programs (12)

Source: Internet
Author: User
Tags strcmp

String functions
C-language string-handling functions
1.puts function

1 // output A string ending with ' /' to the screen 2 Char " Welcome to " ; 3 Char " Linux C Program " ; 4 puts (a); 5 puts (p);

2.gets function

1 // Enter a character array from the terminal, returning the first address of the character array 2 Char string [+]; 3 gets (string); 4 puts (string); 5 // warning:the ' gets ' function is dangerous and should isn't be used. 6 // the system is not recommended to use the Gets method, dangerous

3.strcpy and strncpy

1 #include <string.h>2char *strcpy (charChar *src); 3 Char *strncpy (charchar *src,int n); // The first n characters of the copy 4 // strcpy is a string copy abbreviation, and using both functions must contain string.h, and the return value is Dest 5 // Copied together with ' + '

Replication error Code Demonstration:

1 Char "  " , b[); 2 b = A; 3 // string copying can only use functions such as strcpy

strcpy unsafe, easy to be exploited by hackers, generally with strncpy
Example code:

1 Char " Hello WORLG " ; 2 Char d1[(],d2[); 3 strcpy (d1,s); 4 strncpy (d2,s,sizeof(s)); 5 // 6 // strncpy Copy Incomplete ... 

4.strcat and Strncat

1 #include <string.h>2char *strcat (charChar *src); 3 Char *strncat (charchar *src,int n); // The first n characters of the copy 4 // append the input src to the tail of the dest 5 // strcat Not Safe

5.STRCMP and STRNCMP

1 #include <string.h>2char *strcmp (charchar *s2); // comparison of two strings 3 Char *strncmp (charchar *s2,int n); // Compare top N strings 4 // when different characters are first encountered, the difference of S1-S2 is the return value

6.strlen

1 #include <string.h> 2 // returns the actual length of the string, does not include the Terminator '% ', sizeof (s) The calculation will contain a Terminator

Abbreviations for 7.STRLWR and strupr//string lower and string upper

8.strstr and STRCHR

1#include <string.h>2 Char*STRSTR (Char*S1,Char*S2);//looking for S2 in S1, returning the first pointer to the S2 position, no return null found3 Char*STRCHR (Char*S1,Charc);//S1 looks for the position where C first appears, returns a pointer, and does not return null4 //----5#include <stdio.h>6#include <string.h>7 8 intMain () {9         Char*S1 ="liunx C Program", *s2="Unx",*p;Ten  Onep =strstr (S1,S2); A         if(P! =NULL) { -printf"%s\n", p); -}Else{ theprintf"Not found!"); -         } -  -P= STRCHR (S1,'C'); +         if(P! =NULL) { -printf"%s\n", p); +}Else{ Aprintf"Not found!"); at         } -         return 0; -}

Linux C Programs (12)

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.