What Is an Array String?

A character array is an array used to store character data. The general form of its definition is: char array name [data length]. A character array is used to store characters or strings. An element in the character array stores a character, which occupies one byte in memory. There is no string type in C. Strings are stored in character arrays.

For storage
Character array
You can get a character by referencing an element in the character array.
The reference form of the array is:
Array name [subscript]
[Example 5-8] Enter "I like playing basketball" and display it.
#include <stdio.h>
int main (void)
{
char a [26] = "I like playing basketball";
int i;
for (i = 0; i <26; i ++)
printf ("% c", a [i]);
}
The result is: I like playing basketball [2]
String and end-of-string flags
In C, strings are treated as arrays of characters. In order to determine the actual length of a string, the C language specifies a "string end flag" with the character '\ 0' as the end flag [1]
  1. Character array input
    (1) Use getchar () or scanf () '% c' format character to perform character assignment on the array. For example, for the array a [10]: assign using getchar ():
    for (i = 0; i <10; i ++)
    a [i] = getchar ();
    Assign with scanf ():
    for (i = 0; i <10; i ++)
    scanf ("% c", & a [i]);
    (2) Assign values to the array using the '% s' format of scanf (). Still for array a [10]:
    scanf ("% s", a);
    or
    scanf ("% s", & a [0]);
    When you enter "C program" and press enter, the a array will automatically contain a string "C program" ending with "\ 0".
  2. Character array output
    (1) Use the '% c' format character of putchar () or printf () to perform character assignment on the array. For example, for the array a [10]: assign using putchar ():
    for (i = 0; i <10; i ++)
    a [i] = putchar ();
    Assign using printf ():
    for (i = 0; i <10; i ++)
    printf ("% c", a [i]);
    The output is:
    c program [2]
    Example 6.8 Enter a line of characters, count how many words are in them, and separate them with spaces.
    #include <stdio.h>
    void main ()
    {
    char string [81];
    int i, num = 0, word = 0;
    char c;
    gets (string);
    for (i = 0; (c = string [i])! = '\ 0'; i ++)
    if (c == '') word = 0;
    else if (word == 0)
    {
    word = 1;
    num ++;
    }
    printf ("There are% d words in this line. \ n", num);
    }
    operation result
    I am a boy.
    There are 4 words in this line. [1]

    IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?