ios-c_day11___ string

Source: Internet
Author: User
Tags sprintf strtok

2015.2.2

#include <stdio.h>

#include <ctype.h>

Functions for character manipulation

int isalnum (int);

int isalpha (int);

int isdigit (int);

int isgraph (int);

int islower (int);

int ispunct (int);

int isspace (int);//Determines whether it is a space

int isupper (int);

int isxdigit (int);

int tolower (int);

int toupper (int);

int digittoint (int);

int ishexnumber (int);

int isnumber (int);

int main (int argc, const char * argv[]) {

//

printf ("isalnum =%d\n", Isalnum (' a '));//Determine if it is a numeric character or an English letter

printf ("Isalpha =%d\n", Isalpha (' * '));//Determine if it is an English letter

printf ("IsDigit =%d\n", IsDigit (' & '));//Determine if the numeric character

printf ("Isgraph =%d\n", isgraph (' \ n '));//Determine if it is a visible character

printf ("Islower =%d\n", islower (' * '));//Determines whether it is a lowercase letter

printf ("ispunct =%d\n", ispunct (';')); /Judging whether it is a punctuation mark

printf ("Isupper =%d\n", Isupper (' A '));//Determine if uppercase

printf ("Isxdigit =%d\n", Isxdigit (' a '));//Determine if it is a hexadecimal character

printf ("ToLower =%c\n", ToLower (' A '));//Convert to lowercase

printf ("ToUpper =%c\n", ToUpper (' a '));//Convert to uppercase

printf ("Digittoint =%d\n", Digittoint (' F '));//convert hexadecimal characters to integer data

printf ("Ishexnumber =%d\n", Ishexnumber (' 9 '));//Same as Isxdigit function

printf ("Isnumber =%d\n", Isnumber (' 9 '));//Same as isdigit function

return 0;

//}

String

"Hello World"

1. Double quotation marks contain

2. Each element in a string consumes one byte of memory space

3. A hidden character at the end of the string ' \ s '

//

The print string corresponds to the first address of the string to be printed with%s

storing strings in a character array, or requesting memory

Gets ()//Enter a string

Puts ()//Output a string

int main (int argc,const char *argv[])

//{

Char str[12]= "Hello World";

printf ("%s\n", str);//%s print string encountered ' \ n ' End

//

Char str2[100];

Get (STR2);//meet ' \ n ' End

Puts (STR2);

//

return 0;

//}

Char str1[100]= "Hello World"

Char *str2= "Hello World";

int main (int argc,const char *argv[])

//{

Char str1[100]= "Hello World";

Char *str2= "Hello World";//str2 points to the constant area, the contents of the constant area are read-only and cannot be modified

//

*STR2 = ' a ';

Str1[0]= ' a ';

printf ("%s\n", str1);

return 0;

//}

String length (usually referred to as string valid length)

"Hello World"

Valid length does not contain the ' + ' at the end of the string

sizeof strlen

Difference:

1.sizeof is an operator, strlen is a function

2.sizeof variables or constants occupy memory space size, strlen to find the number of valid characters, the operand is a string

The 3.sizeof operator is the concept of a compilation hierarchy, and the strlen function is the concept of a running hierarchy

#include <string.h>

size_t <==> unsigned long

size_t My_strlen (const char *SRC)

//{

if (!SRC) {

return 0;

//    }

int len=0;

while (*src! = ')} {

len++;

src++;

////    }

for (; *src;) {

len++;

src++;

//    }

return Len;

//}

//

int main (int argc, char *argv[])

//{

Char str[100]= "Qian Feng";

char *pstr = str;

printf ("%ld\n", sizeof (str));

//

printf ("%ld\n", My_strlen (str));

//

printf ("%ld\n", sizeof (PSTR));//The amount of memory space occupied by the pointer variable

printf ("%ld\n", strlen (PSTR));

//

return 0;

//}

string comparison function

int strcmp (const char *, const char *);

int strncmp (const char *, const char *, size_t);

size_t: Limit the maximum number of comparison characters

return value greater than 0 str1 > str2

equals 0 str1 = = str2

Less than 0 str1 < str2

int my_strcmp (const char *STR1, const char *STR2)

//{

while (*str1==*str2 && *str1 && *str2) {

str1++;

str2++;

//    }

return *STR1-*STR2;

//}

//

int main (int argc,const char *argv[])

//{

Char *str1 = "ABCDEFGH";

char *STR2 = "abc";

//

int ret = MY_STRCMP (str1, str2);

printf ("ret =%d\n", ret);

//

ret = strncmp (str1, str2, 7);

printf ("ret =%d\n", ret);

//

return 0;

//}

String Lookup

Strchr

Char *STRCHR (const char *, int);

Find the first occurrence of a character in the original string from left to right, no return null found

Strrchr

Char *STRRCHR (const char *, int);

Reverse lookup character, find the first occurrence of the position returned, cannot find return null

Char *my_strchr (const char *SRC, char ch)

{

while (*SRC) {

if (*src==ch) {

Return (char *) src;

}

src++;

}

return NULL;

}

Char *my_strrchr (const char *SRC, char ch)

{

size_t len = strlen (src);

for (long I=len; i>=0; i--) {

if (* (src+i) ==ch) {

Return (char *) (src+i);

}

}

return NULL;

}

int main (int argc,const char *argv[])

//{

char *str = "Hello World";

//

printf ("%s\n", MY_STRCHR (str, ' o '));

//

printf ("%s\n", MY_STRRCHR (str, ' o '));

//

//

return 0;

//}

String copy

Char *strcpy (char *dest, const char *src);

The memory that dest and SRC point to cannot have overlapping parts

Dest space enough to accommodate the SRC string

Char *strncpy (char *dest, const char *SRC, size_t len);

Len limits the number of characters that can be copied to the Dest buffer

len = sizeof (dest)-1

Char *my_strcpy (char *dest, const char *SRC)

{

char *temp = dest;

while (*SRC) {

*temp = *SRC;

temp++;

src++;

}

*temp = ' + ';

return dest;

}

Char *my_strncpy (char *dest, const char *SRC, size_t len)

{

size_t i=0;

char *temp = dest;

while (*src && I<len) {

*temp = *SRC;

temp++;

src++;

i++;

}

if (I<len) {

*temp = ' + ';

}

return dest;

}

int main (int argc, const char *argv[])

//{

Char dest[100]= "Qianfengabcdef";

Char *src = "HelloWorld";

printf ("%s\n", my_strcpy (dest, SRC));

printf ("%s\n", dest);

printf ("%s\n", strcpy (dest, &dest[4])),//strcpy-manipulated string memory does not overlap

//

printf ("%s\n", my_strncpy (dest, SRC, 100-1));

return 0;

//}

string concatenation

Char *strcat (char *dest, const char *src);

1. Determine that the remaining memory space of the dest is sufficient to accommodate the SRC string

Char *strncat (char *dest, const char *SRC, size_t len);

len = sizeof (dest)-strlen (dest)-1;

Len allows a maximum number of characters to be stitched into the dest buffer, and after stitching, add ' + ' at the end of the new string;

Char *my_strcat (char *dest, const char *SRC)

{

size_t len = strlen (dest);

int i=0;

while (*SRC) {

* (dest+len+i) = *SRC;

src++;

i++;

}

* (dest+len+i) = ' + ';

return dest;

}

Char *my_strncat (char *dest, const char *SRC, size_t len)

{

size_t i=0;

size_t size = strlen (dest);

char *temp = dest+size;

while (*src && I<len) {

*temp = *SRC;

temp++;

src++;

i++;

}

*temp = ' + ';

return dest;

}

int main (int argc,const char *argv[])

//{

Char dest[21]= "Hello World";

dest[20]= ' A ';

dest[16]= ' W ';

Char *src = "Qian Feng";

printf ("%s\n", My_strcat (dest, SRC));

//

printf ("%s\n", My_strncat (dest, src,4));

//

return 0;

//}

String segmentation

Strtok

Char *strtok (char *src, const char *demi);

int main (int argc,const char *argv[])

//{

Char src[100]= "Root:qian;:feng,:jiao;yu";

Char *ps = strtok (src, ";,:");//The bottom layer divides the delimiter into '

printf ("%p%s\n", ps,ps);//delimiters must be enclosed in double quotes, even if there is only one character

printf ("%p%s\n", SRC,SRC);

//

PS = Strtok (NULL, ":;,");

printf ("%s\n", PS);

//

PS = Strtok (NULL, ":;,");

printf ("%s\n", PS);

//

PS = Strtok (NULL, ":;,");

printf ("%s\n", PS);

//

PS = Strtok (NULL, ":;,");

printf ("%s\n", PS);

//

return 0;

//}

int main (int argc,const char *argv[])

//{

Char str[100]= "Root:qian;:feng,:jiao;yu";

//

char *pstr = str;

while ((Pstr = Strtok (Pstr, ":;,"))) {

printf ("%s\n", pstr);

Pstr = NULL;

//    }

//

return 0;

//}

Find a child string

Strstr

Char *strstr (const char *SRC, const char *substr);

Returns the address of the first occurrence of a substring in the original string, without which the substring in the original string returns null

Char *my_strstr (const char *SRC, const char *SUBSTR)

{

Char *sub = (char *) substr;

while (*SRC) {

Char *temp = (char *) src;

while (*src++ = = *substr++) {

if (*substr = = ' + ') {

return temp;

}

}

src = temp+1;

substr = Sub;

}

return NULL;

}

int main (int argc,const char *argv[])

//{

Char src[100]= "Hello Wwworldqian World Feng";

printf ("%s\n", My_strstr (SRC, "World"));

//

return 0;

//}

"Qianarefengarejiaoareyu"

"Is"

Qian Feng Jiao Yu

int main (int argc,const char *argv[])

//{

Char str[100]= "Qianarefengarejiaoareyu";

Char *p,*q;

p = q = str;

int len = strlen ("is");

while (P = strstr (P, "is")) {

*p = ' + ';

printf ("%s\n", Q);

p+= Len;

Q = p;

//    }

if (*q) {

printf ("%s\n", Q);

//    }

return 0;

//}

SSCANF sprintf

int sscanf (const char *, const char *, ...)

int sprintf (char *, const char *, ...);

int main (int argc,const char *argv[])

{

Char str[100]= "$GPGGA, 4250.5589,s,14718.5084,e";

Char header[100]={};

Char Ch1, CH2;

float South;

float East;

SSCANF (str, "%6s,%f,%c,%f,%c", HEADER,&SOUTH,&CH1,&EAST,&CH2);

printf ("%.2f%.2f\n", South, east);

Char buf[100];

sprintf (buf, "%d***%d", 12,45);

printf ("%s\n", buf);

return 0;

}

ios-c_day11___ string

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.