Home > Article > Backend Development > How to enter a character array in c++
How to enter a character array in C
There are many ways to enter a character array in C:
cin.getline()
cin.getline(array_name, size, delimiter);
Parameters:
array_name
: The name of the character array size
: The size of the character array delimiter
: Enter the terminating character (such as newline character) For example:
<code class="cpp">char str[100]; cin.getline(str, 100, '\n');</code>
cin.get( )
cin.get(array_name[index]);
Parameters :
array_name[index]
: The index position of the character array For example:
<code class="cpp">char str[100]; for (int i = 0; i < 100; i++) { cin.get(str[i]); if (str[i] == '\n') break; }</code>
gets()
gets(array_name);
Parameters:
array_name
: The name of the character array Note: gets() function is unsafe because if the input is too long, it will cause buffer overflow.
fgets()
fgets(array_name, size, stream);
Parameters:
array_name
: The name of the character array size
: Characters The size of the arraystream
: Input stream##For example:
<code class="cpp">char str[100]; fgets(str, 100, stdin);</code>
The above is the detailed content of How to enter a character array in c++. For more information, please follow other related articles on the PHP Chinese website!