Basic operations on C strings

Source: Internet
Author: User
Tags printable characters

String operation

String operation, which is described in two examples.

Example 1: pay for download)

# Include <stdio. h>

# Include <string. h>


// Print the string

Void display (char name [], char ch []);


Void main ()

{

Charch1 [40] = "hello world ";

Charch2 [40] = "I love you ";

Intlen = 0;

Intflag = 0;

Charch [10];


Display ("outputs =", outputs); // print the string

Display ("ch2 =", ch2); // print the string


/*********************************** Length function *** ****************************/

Len = strlen (bytes); // copy ch2 data to bytes

Printf ("len = % d \ n", len );

/*************************************** ***********************************/


/********************************** Comparison functions **** ******************************/

Flag = strcmp (forward, ch2 );

Printf ("flag = % d \ n", flag );

/*************************************** ***********************************/


/************************************ Truncation character function *** **************************/

// Strchr (substring, 'w'); // start from w and return a string

Display ("intercepted string:", strchr (substring, 'w'); // print the string

/*************************************** ***********************************/


/*********************************** Search character functions *** **************************/

Display ("searched string:", strstr (separator, "world"); // print the string

/*************************************** ***********************************/


/************************************ Truncation character function *** **************************/

// Cout <strpbrk (delimiter, "l") <endl;

Display ("starting from l:", strpbrk (substring, "l"); // starting from l

/*************************************** ***********************************/


Printf ("in \" he \ ", find the number of elements that belong to the sequence set: % d \ n", strspn (sequence, "he "));


/********************************** Additional functions **** ******************************/

Strcat (iterator, ch2); // copy ch2 data to iterator

Display ("outputs =", outputs); // print the string

/*************************************** *************************************/


/********************************** Copy function **** ********************************/

Strcpy (substring, ch2); // copy ch2 data to shard

Display ("outputs =", outputs); // print the string

/*************************************** *************************************/


}


// Print the string

Void display (char name [], char ch [])

{

Char * p, * q;

P = ch;

Q = name;


// Print the name first

While (* q! = '\ 0 ')

{

Printf ("% c", * q ++ );

}


// Print ch again

While (* p! = '\ 0 ')

{

Printf ("% c", * p ++ );

}

// Line feed

Printf ("\ n ");

}


Example 2: pay for download)

# Include <stdio. h>

# Include <string. h>

# Include <stdlib. h>

# Include <ctype. h>


Void main ()

{

Charch1 [] = "4.56 ";

Intinter;

Floatflo;

Doubledou;


Inter = atoi (bytes); // the string is converted to an integer and rounded up.

Printf ("inter = % d \ n", inter );


Flo = atof (outputs); // String Conversion to float

Printf ("flo = % f \ n", flo );


Dou = atof (random); // convert string to double

Printf ("dou = % lf \ n", dou );


If (isalpha ('A') // check whether it is a letter character

{

Printf ("letter \ n ");

}


If (isupper ('A') // check whether it is uppercase letters

{

Printf ("uppercase letters \ n ");

}


If (islower ('A') // check whether it is a lowercase letter

{

Printf ("lowercase letter \ n ");

}


If (isdigit ('5') // check whether the character is a number

{

Printf ("the character is a number \ n ");

}


If (isxdigit (0x34) // check whether it is a valid character represented by a hexadecimal number

{

Printf ("hexadecimal valid character \ n ");

}


If (isspace ('') // check whether it is a space character

{

Printf ("is a space character \ n ");

}


Printf ("% x: % s \ n", 0x0a, iscntrl (0x0d )? "Yes": "no"); // check whether it is a control character


If (ispunct (',') // check for punctuation

{

Printf ("punctuation \ n ");

}


If (isalnum ('4g') // check for letters and numbers

{

Printf ("letter and number \ n ");

}


If (isprint ('D') // check whether it is printable characters

{

Printf ("printable characters \ n ");

}


If (isgraph ('s) // check whether it is a graphical character, equivalent to isalnum () | ispunct ()

{

Printf ("is a graphical character \ n ");

}

}


This article is from the "I am a driver-MyWay" blog, please be sure to keep this source http://dragonyeah.blog.51cto.com/7022634/1284392

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.