C-language string processing

Source: Internet
Author: User

1) C-language string:

#include <stdio.h>

#include <string.h>

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

Char ch[] = {' A ', ' B ', ' C ', ' n '};

Char ch1[] = {' x ', ' y ', ' z '};

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

Output XYZABC, since the output from the memory address is high to low

Defined at the high address, after the low address defined

Char ch[] = "ABC"; So there's no problem.

Puts (CH);

Puts (&ch[1]);

Output ABC, can be wrapped automatically

Gets (CH);

Puts (CH);

Using the get will give a warning

Unsafe: The array length is 100, but if you enter 120 characters, there is an out-of-bounds

can accept Spaces

strcat (CH, ch1);

Puts (CH);

strcat (Oldstr, NEWSTR);

The length of the older should be sufficient to preserve the OLDSTR+NEWSTR

Newstr the last word of oldstr to cover the

return 0;

}

Input and output of strings

#include <stdio.h>

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

Char ch[] = "IAS";

View string For more trouble, you can output a string with%s

Starts from the given address and outputs the character until it stops.

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

Output IAS

printf ("%s\n", &ch[1]);

Output as

Char str3[10];

Accepts a string from the keyboard and is stored in an array of characters in the first address of STR

scanf ("%s", STR3);

printf ("%s", STR3);

If you enter in%s format, note the space problem

If the input string has a space, the characters after the space cannot be received for saving

The length of the input string is less than the length of the array

return 0;

}

2) C language pointers:

int *ptr = &x; is actually the following two statement combinations:

int *ptr;

PTR = &x;

/* Wild pointers can not be arbitrarily modified content, easy to trigger the system to crash, the teacher said that do not know is true, a little doubt, do compilers are stupid people?

int *p;

*p = 98;

Two-level pointers and multi-level pointers

/*

Pointer to reverse array

/* #include <stdio.h>

void swap (int *i, int *j) {

int temp;

while (I < j) {

temp = *i;

*i = *j;

*j = temp;

i++;

j--;

}

}

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

int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int *i = &a[0];

int *j = &a[9];

Swap (I, j);

for (int i = 0; I <= 9; i++) {

printf ("%d", a[i]);

}

return 0;

}

3) Fget and fputs functions are used:

Secure fgets function

char ch [10];

Fgets (ch, sizeof (CH), stdin);

can automatically intercept the appropriate length according to the length of the array to save

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

Fgets (array name, array size, standard keyboard input);

Reads a string from the input buffer into a character array

When the length of the input string is greater than the length of the array, the last element of the array automatically becomes

Accepts a line break when the input length is less than the array length

strlen (CH) does not contain

Fputs is not automatically wrapped.

Puts can be wrapped automatically.

Using gets () and scanf () are unsafe

Gets () can receive a space, but scanf () cannot receive a space after a space is not accepted

printf ()%s cannot be wrapped automatically

Puts () can be wrapped automatically.

Fputs cannot be wrapped automatically.

C-language string processing

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.