Linux C Programming Learning 6---String processing, data conversion

Source: Internet
Author: User
Tags strcmp strtok uppercase letter

1. String
The application can be divided into numerical calculation, non-numerical calculation and input-output operation according to its function. A large proportion of non-numerical computing programs, the core of which is string processing
  1.1. Character test
1.1.1. Test whether the character is an English letter
int isalpha (int c). If C is an English letter, a value other than 0 is returned, otherwise the return value is 0
1.1.2. Test whether a character is a number
int isdigit (int c). If C is a number, it returns a value other than 0, otherwise the return value is 0
  1.2. String initialization
In C, a string is treated as a character array, corresponding to a contiguous area in memory
You can use void *memset (void *buffer, int c, int count) to initialize this contiguous memory.
Buffer is the pointer to the first address of the contiguous memory we want to initialize
The parameter c is used to indicate which character to initialize the memory to, and if C is 0, the memory is zeroed
Parameter count, set the number of bytes in memory
Example

#include <stdio.h> #include <string.h>int main () {char s[]= "Hello World";p rintf ("%s\n", s); memset (S, ' H ', 5) ;p rintf ("%s\n", s); return 0;}

    The output is:

Hello World
hhhhh World
1.3. Copy of the string
1 .3.1. Char *strcpy (char *dest, char *src)
Copies the string src points to the memory pointed to by Dest.
Note: These two parameters point to a memory area that is not allowed to overlap the return value of the
function is a pointer to Dest
1.3.2. Char *STRD The return value of the up (char *s)
function is a pointer to the copied string, and the area of memory pointed to is automatically assigned by the system
1.3.3. void *memcpy (void *dest, void *src, unsigned int c Ount)
Copies the memory area pointed to by the parameter src to the memory area pointed to by Dest
The memory areas that the two pointers point to are also not overlapping function is different from the pointer to Dest
and strcpy: After memcpy the pointer to the source memory and target memory, the memory of the specified size (unsigned int count) is copied without checking the contents of the memory, including the string's knot Shing, and the previous strcpy function is to stop copying
immediately after encountering the string terminator so memcpy does not treat the argument as a string
1.3.4. void *memmove (void *dest, const void *s RC, size_t N)
Copies the area of memory pointed to by the parameter src to the memory area pointed to by Dest

example

#include <stdio.h> #include <string.h>int main () {char s[] = "Linux C Programming";p rintf ("%s\n", s); Memmove ( S, S+6, strlen (s)-6); S[strlen (s)-6] = ';p rintf ("%s\n", s); return 0;}

      The output is

Linux C Programming
C programming
1.4. String comparison / span>
1.4.1. int strcmp (const char *S1, const char *S2)
compares the string that the parameter S1 and S2 points to, based on the ASCII code value of the character, first subtracting the first character of S1 The first character of the S2, if the same, continues to compare the second character, so that it is compared. If the string is the same, the return value is 0, and if it is not the same, the difference between the ASCII codes of the characters that are not identical is returned
1.4.2. int strncmp (const char *S1, const char * s2, size_t N)
Comparison The first n bytes of the string pointed to by S1 and S2
1.4.3. int strcasecmp (const char *S1, const char *S2)
are similar to strcmp, but are case-insensitive during comparison
1.4.4. int strncasecmp (const char *S1, const char *S2, size_t N); The
is similar to strncmp, but is ignored in the process of comparison
1.4.5. int memcmp (const void *S1, const void *S2, size_t N)
Compare S1 and S The first n bytes of the memory space pointed to by 2, but it does not consider the S1 and S2 as strings in comparison, and does not take into account the string terminator in the memory area
1.5. Character/ String Lookup
1.5.1. Char *index (const char *s, int c)
in the string to which the parameter S1 is pointing, look for the character C in the previous direction, and if you find where C first appears, return A pointer to this location that returns a null
example

If the character is not found

#include <stdio.h> #include <string.h>int main () {char s[] = "Hello world"; char *p;p = index (S, ' W ');p rintf ("%s \ n ", p); return 0;}

      The output is

World
1.5.2. Char *rindex (const char *s, int c)
Similar to index, but looking forward from behind
1.5.3. Char *strchr (const char *s, int c)
Similar to index, but cannot find the character ' \ S '. Can be used in the form of index (s, '% '), but cannot be used with STRCHR (s, ' + ')
1.5.4. Char strrctr (const char *s, int c)
Similar to Rindex, but cannot find the character ' \ S '. Can be used in the form of Rindex (s, '% '), but not with STRRCHR (s, ' + ')
1.5.5. Char *strstr (const char *haystack, const char *needle)
Finds the string needle in the string haystack and returns the first occurrence, and returns a null pointer if it is not found
1.6. String Connection Split
1.6.1. Char *strcat (char *dest, const char *SRC)
Connects the string pointed to by SRC to the string pointed to by Dest, returning the starting address of the target pointer
Note that the target string must have sufficient buffers, or overflow will occur
Example

#include <stdio.h> #include <string.h> #define BUFFER_SIZE 64int Main () {char s[buffer_size] = "Orld;char d[ Buffer_size] = "Hello w"; Strcat (d, s);p rintf ("%s\n", s);p rintf ("%s\n", d); return 0;}

The output is

Orld
Hello World
1.6.2. Char *strncat (char *dest, const char *SRC, size_t N)
Extracts n bytes from SRC and connects to the string pointed to by Dest.
If the first n bytes of Src do not appear in '/', then a ' + ' is automatically added at the end
1.6.3. Char *strtok (char *str, const char *delim)
The STR string is segmented using the Delim symbol. If any one delimiter is found in the string str, it is changed to the terminator of the string
Example

#include <strio.h> #include <string.h>int main () {char str[] = "Linux C programming"; char *p;p = strtok (str, "") ; while (P! = NULL) {printf ("%s\n", p);p = Strtok (NULL, "");} printf ("str:"%s\n ", str); return 0;}

The output is

C
Programming
Str:linux

2. Data conversion
Data transformations include conversions between letters of case, strings and integers, and floating-point numbers
  2.1. Letter Case Conversion
int toupper (int c) converts the lowercase English letter to uppercase and returns the corresponding uppercase letter if C is lowercase, otherwise returns the original value
int tolower (int c) converts uppercase letters to lowercase, similar in principle to the one above
  2.2. String conversions
Implementing a conversion between a string and an integer, floating-point number
2.2.1. Converting a string to an integer
int atoi (const char *nptr)
Long ATOL (const char * nptr)
The two functions first scan the string until a number or sign appears to start the conversion, and then a non-numeric or string terminator stops the conversion and returns the result
Only 10-binary strings can be converted
Example

#include <stdio.h> #include <string.h>int main () {char a[] = " -100"; char b[] = "0x20"; int c;c = Atoi (a) + atoi (b); printf ("c =%d\n", c); return 0;}

  

The output is
-100
Because, convert a to-100, and B is 0x20, convert 0 to 0 first, and then stop conversion when X is encountered
2.2.2. Converting a string to a floating-point number
Double atof (const char *nptr)
The function first scans the string until a number or sign starts to convert, and then a non-numeric or string terminator stops the conversion and returns the result
In this function, the string can contain + 、-、 the decimal point, E, E (E, E, the exponential portion of the floating-point number)
2.2.3. Converting floating-point numbers to strings
Char *GCVT (double number, size_t ndigits, Char *buf)
The first parameter is the floating-point number to convert
The second parameter is the number of digits to display
The third parameter is a buffer to hold the final result

Linux C Programming Learning 6---String processing, data conversion

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.