Dark Horse Programmer's String (C language)

Source: Internet
Author: User

basic use of stringsDefine the method:

Char name[10] = "Jack"; at this time the string contains ' J ', ' A ', ' C ', ' k ', ' + '

10 in brackets means that the string holds a maximum of 10 characters

The output of the string

1.printf ("Jack");

2.printf (name); //Send array to printf statement output warning appears

Both methods output the same result, but the printf statement only recognized string constants by default, so the second method would be compiled with a warning .

Storage details for strings:

The array name occupies 8 bytes, containing three elements of ' I ', ' t ', ' + '

Char name[8] = "it";

Char Name2[8] = {' I ', ' t ', ' n '};

Char Name3[8] = {' I ', ' t ', 0};

Three different writing effects

Note: Because the ASCII value of ' + ' is 0, the ' + ' is equal to 0

Modifying the values of elements within an array

NAME[1] = ' Q '; //Change the value of the 2nd element in the array to Q;

The role of

The end flag for the string

#include <stdio.h>  int main () {  char name[] = "it";  Char name2[] ={' o ', ' K '};  printf ("%s\n", name2);  return 0;}
Running Result: Okit

The data memory is as follows

strlen function

Function: Used to calculate the length of the string, the Strlen function declaration in the <string.h> file, calculated that the number of characters is not the word count, the calculated characters do not include the tail of "\"

#include <stdio.h>  #include <string.h>int main () {char name[] = "haha";                 //define an array of strings int size1 =strlen (name);            //The length of the computed string      does not contain "size2"int =sizeof (name);             //Calculate the number of bytes occupied by the string, including "\"        printf ("%d\t%d\n", size1,size2);      //Output        return 0;}
Run Result: 4 5
Code Exercise 1:
//one-dimensional array char  name[] = "haha"; char name1[] = "Rose"; char name2[] = "Jim"; char name3[] = "Jake" ; Two-dimensional array //First method char name[2][10] = {"Jake", "Rose"}; //second method char name[2][10] = {{' J ', ' A ', ' k ', ' e ', '};{' R ', ' O ', ' s ', ' e ', ' n '}; };        return 0;}
Code Exercise 2:
#include <stdio.h> #include <string.h>/* Write a function char_contains (char Str[],char c), if the string contains the character C in Str, The value 1 is returned, otherwise the value 0.*/int char_contains (char Str[],char c) {int i;  Iterates through all elements within the array to detect if the array contains characters Cfor (i = 0;i < strlen (str); i++) {if (Str[i] ==c) {//Returns a value 1return 1;}} return 0;} int main () {   char  str[]= "itcast"; int result = Char_contains (str, ' C ');p rintf ("%d\n", result); return 0;}

Dark Horse Programmer's String (C language)

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.