Fgets (), fputs (), string Function

Source: Internet
Author: User
# Include <stdio. h> # define Max 81int main () {char name [Max]; char * PTR; printf ("Hi, what is your name? \ N "); PTR = fgets (name, Max, stdin); printf (" % s? Ah! % S! \ N ", name, PTR); Return 0;}/* Because fgets stores the linefeed in the string, the * line break * running result is displayed each time the string is displayed: * Hi, what is your name? Tonytony? Ah! Tony! */

Scanf () supports inbound and outbound strings, and can also limit the input length, for example, scanf ("% 5 S % 10 s", name1, name2)

The puts () function only needs to provide the address of the string parameter. When displaying the string, it automatically adds a line break after it. Make sure the output is a string.

Fputs corresponds to fgets (), but the second parameter is required to indicate the file to be written. You can use stout as a parameter to display the output. fgets () stores the linefeed in the input, fputs () does not add line breaks for the output.

The strlen () function is used to evaluate the length of a string. The strcat () function is used to connect two strings. The latter and the string are connected to the previous string. the return value is the value of the first parameter.

Function: add the first n characters of the string referred to by Src to the end of DeST (overwrite '\ 0' at the end of DEST) and add' \ 0 '.

Note: The memory areas specified by Src and DEST cannot overlap and the Dest must have enough space to hold the SRC string. a pointer to the Dest is returned.

Strncat prototype: extern char * strncat (char * DEST, char * SRC, int N). One function is to copy a specified number of characters.

Strcpy () function implementation code:

Extern char * strcpy (char * DEST, const char * SRC );

Copy the string starting from the SRC address and containing the null terminator to the address space starting with DEST, which is equivalent to a value, similar to a = B, which is a string value assignment;

However, if the real parameter is only a string pointer and is not initialized, an error will occur because the allocation size is different, which may cause an error.

# Include <stdio. h> # include <stdlib. h> char * strcpy (char * S1, const char * S2) // you must add const {int I = 0; // char * P = S1, in this way, while (s2 [I]! = '\ 0') // * S2! = '\ 0' {S1 [I] = S2 [I]; // * S1 ++ = * S2 ++; the pointer is always going backwards, therefore, the replication fails, but I ++;} S1 [I] = '\ 0'; // * S1 =' \ 0' // S1 = P; // This ensures pointer synchronization. Because the pointer goes to the front, you need to go back to the original address return S1;} int main (void) {char a [] = "Hello! "; Char B [10]; puts (strcpy (B, A); printf (" % s ", strcpy (B, A); // puts (B ); return 0 ;}

The strcmp () function compares two strings instead of arrays. the return value is of the int type.

Function prototype: int strncmp (char * str1, char * str2, int maxlen );

This function compares the first maxlen characters of str1 and str2. If the first maxlen bytes are completely equal, the return value is 0. If str1 [N] And str2 [N] appear during the comparison of the previous maxlen bytes, returns (str1 [N]-str2 [N]).

Char * strchr (const char * s, int C); this function returns a position pointing to the first pointer of C in string S.

Returns NULL if it does not exist.

Char * strrchr (const char * s, char c). This function returns a pointer that is the last occurrence of character C. If it exists every day, a null pointer is returned.

Char * strpbrk (const char * S1, const char * S2). This function returns the first position of any character in S1 that stores the S2 string. If no,

Returns a null pointer.

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.