C String function

Source: Internet
Author: User

C Edit strcpyPrototype: extern char *strcpy (char *dest,char *src), usage: #include <string.h> function: Copy the string from the end of the NUL referred to by SRC to the array referred to by Dest. Returns a pointer to the character (NUL) at the end of the dest. Example://Strcpy.c#include <syslib.h> #include <string.h>main () {char *s= "Golden Global View"; Char D[20];clrscr (); strcpy (d,s);p rintf ("%s", d); GetChar (); return 0;}strcatPrototype: extern char *strcat (char *dest,char *src), usage: #include <string.h> function: Add the string src refers to at the end of the Dest (overwrite the dest at the end) and add ' '. Returns a pointer to the dest. Example://Strcat.c#include <syslib.h> #include <string.h>main () {char d[20]= "Golden Global"; char *s= "View"; CLRSCR (); strcat (d,s);p rintf ("%s", d); GetChar (); return 0;}strlenPrototype: extern int strlen (char *s); Usage: #include <string.h> function: Calculates the length of the string s description: Returns the length of S, excluding the Terminator null. Example://Strlen.c#include <syslib.h> #include <string.h>main () {char *s= "Golden Global View"; CLRSCR ();p rintf ( "%s has%d chars", S,strlen (s)); GetChar (); return 0;}StrncatPrototype: extern char *strncat (char *dest,char *src,int N), usage: #include <string.h> function: Adds the first n characters of the string src refers to at the end of the Dest ( Overwrite the ' dest ' at the end of the range and add '/'. Returns a pointer to the dest. Example://Strncat.c#include <syslib.h> #include <string.h>main () {char d[20]= "Golden Global"; char *s= "View Winide Library "; Clrscr (); Strncat (d,s,5);p rintf ("%s ", d); GetChar (); return 0;}strncpyPrototype: extern char *strncpy (char *dest, char *src, int n); Usage: #include <string.h> Function: Copies the first n bytes of a string that is referred to by the NULL as SRC to the array referred to by Dest. Description: If the first n bytes of Src do not contain a null character, the result will not end with a null character. If the src length is less than n bytes, the dest is padded with null until n bytes are copied. The memory areas referred to by SRC and dest cannot overlap and dest must have enough space to accommodate the SRC string. Returns a pointer to the dest. Example://Strncpy.c#include <syslib.h> #include <string.h>main () {char *s= "Golden Global View"; char *d= "Hello, GGV programmers "; Char *p=strdup (s); Clrscr (); TextMode (0x00); Enable 6 lines modestrncpy (D,s,strlen (s));p rintf ("%s\n", D), strncpy (P,s,strlen (d));p rintf ("%s", p); free (p); GetChar (); return 0;}strcspnFunction: Finds the segment usage of the first given character set content in a string: int strcspn (char *str1, char *str2); program example: #include <stdio.h> #include <string.h># Include <alloc.h>int main (void) {char *string1 = "1234567890"; char *string2 = "747DC8"; int length;length = STRCSPN (s Tring1, string2);p rintf ("Character where strings intersect is at position%dn", length); return 0;}StrDupFunction: Copy the string to the new location usage: char *strdup (char *str); program example: #include <stdio.h> #include <string.h> #include <alloc.h& Gt;int Main (void) {char *dup_str, *string = "ABCDE";d up_str = StrDup (String);p rintf ("%sn", dup_str); free (DUP_STR); return 0;}stricmpFunction: Comparison of two string usages in case insensitive manner: int stricmp (char *str1, char *str2); program example: #include <string.h> #include <stdio.h>int main (void) {Char *buf1 = "BBB", *buf2 = "BBB"; int ptr;ptr = STRICMP (Buf2, BUF1), if (ptr > 0) printf ("Buffer 2 is greater than Buffe R 1n "), if (PTR < 0) printf (" Buffer 2 is less than buffer 1n "), if (ptr = = 0) printf (" Buffer 2 equals buffer 1n "); return 0; }strerrorFunction: Returns a pointer to the error message string usage: char *strerror (int errnum); program example: #include <stdio.h> #include <errno.h>int main (void) { Char *buffer;buffer = strerror (errno);p rintf ("Error:%sn", buffer); return 0;}strcmpFunction: Compare one string with another: Intstrcmpi (char *str1, char *str2); program example: #include <string.h> #include <stdio.h>int main ( void) {char *buf1 = "BBB", *buf2 = "BBB"; int ptr;ptr =strcmpi (buf2, BUF1), if (ptr > 0) printf ("Buffer 2 is greater than B Uffer 1n "), if (PTR < 0) printf (" Buffer 2 is less than buffer 1n "), if (ptr = = 0) printf (" Buffer 2 equals buffer 1n "); retur n 0;}strncmpFunction: Compares a part of a string to a part of another string (first n characters) usage: int strncmp (char *str1, char *str2,int maxlen); program example: #include <string.h> #include & Lt;stdio.h>int Main (void) {char *buf1 = "BBBCCC", *buf2 = "BBBCCC"; int ptr;ptr = STRNCMP (buf2,buf1,3); if (ptr > 0) pri NTF ("Buffer 2 is greater than buffer 1n"), if (PTR < 0) printf ("Buffer 2 is less than buffer 1n"), if (ptr = = 0) printf ("Bu Ffer 2 equals buffer 1n "); return 0;}strnicmpFunction: Compare the first n characters of two strings in a case-insensitive manner: intstrnicmp (char *str1, char *str2, unsigned maxlen); program example: #include <string.h> #include &L T;stdio.h>int Main (void) {char *buf1 = "BBBCCC", *buf2 = "BBBCCC"; int ptr;ptr =strnicmp (buf2, BUF1, 3); if (ptr > 0) pr intf ("Buffer 2 is greater than buffer 1n"), if (PTR < 0) printf ("Buffer 2 is less than buffer 1n"), if (ptr = = 0) printf ("B Uffer 2 equals buffer 1n "); return 0;}StrnsetFunction: Sets all characters in a string to the specified character usage: char *strnset (char *str, char ch, unsigned n); program example: #include <stdio.h> #include <string.h >int Main (void) {Char letter = ' x ';p rintf ("string beforestrnset:%sn", String), Strnset (string, letter,);p rintf (" String Afterstrnset:%sn ", string); return 0;}strpbrkFunction: Find character usage in a given character set in a string: Char *strpbrk (char *str1, char *str2); program example: #include <stdio.h> #include <string.h>int ma In (void) {char *string1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char *string2 = "onm"; char *ptr;ptr = STRPBRK (string1, string2); if (PTR) printf ("Strpbrk found first character:%cn", *ptr); elseprintf ("strpbrk didn ' t find character in Setn"); return 0;}STRRCHRFunction: Finds the last occurrence of a specified character in a string usage: char *strrchr (char *str, char c); program example: #include <string.h> #include <stdio.h>int main ( void) {char String[15];char *ptr, C = ' R '; strcpy (String, "This is a string");p tr = STRRCHR (string, c); if (PTR) printf ("The C Haracter%c is at position:%dn ", C, ptr-string); elseprintf (" The character were not Foundn "); return 0;}StrrevFunction: String reversal usage: char *strrev (char *str); program example: #include <string.h> #include <stdio.h>int main (void) {char *forward = " String ";p rintf (" Before Strrev ():%sn ", forward), Strrev (forward);p rintf (" After Strrev ():%sn ", forward); return 0;}StrsetFunction: Sets all characters in a string to the specified character usage: char *strset (char *str, char c); program example: #include <stdio.h> #include <string.h>int main ( void) {char string[10] = "123456789"; char symbol = ' C ';p rintf ("Beforestrset ():%sn", string); Strset (string, symbol); printf ("Afterstrset ():%sn", string); return 0;}strspnFunction: Returns the first character in a string that does not appear in the specified string subscript usage: int strspn (char *str1, char *str2); program example: #include <stdio.h> #include <string.h& gt; #include <alloc.h>int main (void) {char *string1 = "1234567890"; char *string2 = "123DC8"; int length;length = STRSP N (string1, string2);p rintf ("Character where strings differ is at position%dn", length); return 0;}strstrFunction: Finds the first occurrence of a specified string in a string usage: char *strstr (char *str1, char *str2); program example: #include <stdio.h> #include <string.h>int m Ain (void) {char *str1 = "Borland International", *STR2 = "Nation", *ptr;ptr = Strstr (str1, str2);p rintf ("The substring is: %sn ", PTR); return 0;}strtodFunction: Converts a string to a double strtod (char *str, char **endptr); program example: #include <stdio.h> #include <stdlib.h> int main (void) {char input[80], *endptr;double value;printf ("Enter A floating point number:"), gets (input), value = Strtod ( Input, &endptr);p rintf ("The string is%s the number is%LFN", input, value); return 0;}strtokFunction: Finds the word usage delimited by the delimiter specified in the second string: Char *strtok (char *str1, char *str2); program example: #include <string.h> #include <stdio.h >int Main (void) {char input[16] = "Abc,d"; char *p;/* strtok places a NULL Terminatorin front of the token, if found */p = Strtok (Input, ","); if (p) printf ("%sn", p);/* A second call to Strtok using a Nullas the first parameter returns A point Erto the character following the token */p = Strtok (NULL, ","); if (p) printf ("%sn", p); return 0;}StrtolFunction: Convert string to Long integer usage: long strtol (char *str, char **endptr, int base); program example: #include <stdlib.h> #include <stdio.h>int Main (void) {char *string = "87654321", *endptr;long lnumber;/* strtol converts string to Long integer */lnumber = strtol (s Tring, &endptr,;p rintf ("string =%s long =%ldn", string, Lnumber); return 0;}STRUPRFunction: Converts lowercase letters in strings to uppercase usage: char *strupr (char *str); program example: #include <stdio.h> #include <string.h>int main (void) { Char *string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", *ptr;/* converts string to upper case characters */ptr =STRUPR (string);p rintf ("%sn", PTR); return 0;}Swabfunction: Swap byte usage: void swab (char *from, char *to, int nbytes); program example: #include <stdlib.h> #include <stdio.h> #include & Lt;string.h>char source[15] = "Rfna Koblrna d"; char Target[15];int main (void) {swab (source, target, strlen (source)); printf ("This is target:%SN", target); return 0;} Ps:isalpha () is a character function, not a string function,IsalphaPrototype: extern int isalpha (int c); Usage: #include <ctype.h> function: Determines whether the character C is an English letter description: When C is an English letter A-Z or a-Z, returns a value other than 0, otherwise returns zero. Example://isalpha.c#include <syslib.h> #include <ctype.h> #include <stdio.h>main () {int c;clrscr (); Clear Screenprintf ("press a Key"); for (;;) {C=getchar (); Clrscr ();p rintf ("%c:%s Letter", C,isalpha (c)? " Is ': ' not ');} return 0; Just to avoid warnings by compiler}

C String function

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.