C Prime Plus (fifth Edition)---the 11th chapter string and String Functions---2

Source: Internet
Author: User
Tags first string

11-3: String Output

Three standard functions: puts () fputs () printf ()

1.puts () function:

  

#include <stdio.h>#defineDEF "I an A #define string."intMain () {Charstr1[ the] ="An array is initialized to me."; Const Char*STR2 ="A Pointer was initialized to me."; Puts ("I ' m an argument to puts ().");    Puts (DEF);    Puts (STR1);    Puts (STR2); Puts (&str1[5]); Puts (str2+4); return 0;} 

In this program, &str1[5] is the 6th element of the array str1. Str2+4 is the memory unit of I, and the puts function will stop when it encounters a null character.

The 2.fputs () function, which is file-oriented, differs from the puts () function: The second parameter describes the file to be written. At the same time, line breaks are not added automatically at the time of output.

3.printf () function: not explained.

4. Custom input/Output functions:

void put1 (constchar *string) {    while (*string  )        Putchar (*string+ +)}

5. String Functions:

1) strlen () function: Let's look at a function that attempts to shorten a string.

    

#include <stdio.h>#include<string.h>voidFitChar*,unsignedint);intMain () {CharMesg[] ="Hold in to your hats,hackers.";    Puts (MESG); Fit (MESG,7);    Puts (MESG); Puts ("Let's look at some more of the string."); Puts (MESG+8); return 0;} voidFitChar*string, unsignedintsize) {    if(Strlen (string) >size) {        *(string+ size) =' /'; }}

2) strcat () function: accepts two string arguments. The second string is added behind the first string, and then the first string is returned. While the first string changes, the second string is unchanged.

3) Strncat () function: accepts three parameters, the third argument is a number, is the maximum number of characters a function accepts, or a null character is encountered.

4) strcmp () function: This function is used to compare the contents of a string in two arrays.

5) strncmp () function: This function has a third parameter, which is used to limit the number of strings to compare.

  

#include <stdio.h>#include<string.h>#defineListsize 5intMain () {Char*list[listsize] = {        "Astronomy",        "Astounding",        "Astronphysics",        "ostracize",        "ASTERUSM"    }; intCount =0 ; inti;  for(i =0; i < listsize; i++){        if(STRNCMP (List[i),"Astro",5) ==0) {printf ("found:%s\n", List[i]); Count++; }} printf ("The list contained%d words beginning""With astro.\n", Count); return 0;} 

6) strcpy () function and strncpy () function:

  

C Prime Plus (fifth Edition)---the 11th chapter string and String Functions---2

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.