Computer Science, asked by kalechandrakant5811, 1 year ago

Which of these can be used to implement pass input string that uses a character array?

Answers

Answered by prashanth1551
0
String and Character Array

String is a sequence of characters that is treated as a single data item and terminated by null character '\0'. Remember that C language does not support strings as a data type. A string is actually one-dimensional array of characters in C language. These are often used to create meaningful and readable programs.
For example: The string "hello world" contains 12 characters including '\0' character which is automatically added by the compiler at the end of the string.

Declaring and Initializing a string variables

There are different ways to initialize a character array variable.
char name[13] = "StudyTonight"; // valid character array initialization char name[10] = {'L','e','s','s','o','n','s','\0'}; // valid initialization
Remember that when you initialize a character array by listing all of its characters separately then you must supply the '\0' character explicitly.
Some examples of illegal initialization of character array are,
char ch[3] = "hell"; // Illegal char str[4]; str = "hell"; // Illegal

String Input and Output

Input function scanf() can be used with %s format specifier to read a string input from the terminal. But there is one problem with scanf() function, it terminates its input on the first white space it encounters. Therefore if you try to read an input string "Hello World" using scanf() function, it will only read Hello and terminate after encountering white spaces.
However, C supports a format specification known as the edit set conversion code %[..] that can be used to read a line containing a variety of characters, including white spaces.
Similar questions