Write about Character Arrays (or) Strings? Explain how to Declare & Initialize Strings?
https://www.computersprofessor.com/2016/12/write-about-character-arrays-or-strings.html?m=0
|
|||||||||||||||||
String: A string is a sequence
of characters that is treated as a single data item.
Any group of characters
defined in between Double quotation marks is a string constant.
Ex: “ degree college”
If we want to
include a double quote in the
string to be printed, then we may use it with a back slash as:
printf(“\” well Done ! “\” );
Will out put the string “well Done”
The common operations performed on characters
strings include:
. Reading &
writing strings
.
Combining strings together
.
Copying
one string for equality
.
Comparing
strings for equality
.
Extracting
a portion of a string.
|
|||||||||||||||||
Declaring and
initializing string variables:
|
|||||||||||||||||
C does not support strings
as a data type. It allows
us to represent
strings as character
arrays.
A string variable is any valid c variable name & is always declared as a
array of characters.
The general form of declaration of a string variable is :
Syntax: char
string_name [size] ;
The size determines the
number of characters in the string_ name.
Ex: char city [10];
char name[30];
When the compiler assigns
a character string to a
character array , it automatically supplies
a null character (‘\0’) at the
end of the string.
Therefore the size should
be equal to the maximum number of characters in the strings plus one.
Like numeric arrays,
character arrays may be initialized when they are declared.
C permits two forms of
initialization as:
char city [9]= “New York” ;
char city [9]= {‘N’,’E’,’W’,’ ‘,’Y’,’O’,’R’,’K’};
C also permits us to
initialize a character array without specifying the number of elements. In
such cases, the size of the array will be determined automatically, based on
the number of elements initialized.
Ex : char string [ ] = { ‘G’, ‘O’,
‘O’, ‘O’, ‘\0’};
|
|||||||||||||||||
Defines the array string
as a 5 elements array.
We can also declare the
size much larger than the string size in the initialization i.e. char str
[10] = “GOOD”;
In this case, the
computer creates a character array of size 10, places the value “GOOD” in it,
terminates with the null character, and initializes all other elements To
NULL.
|
|||||||||||||||||
The following declaration
is illegal.
char str [3]= “Good”;
This will result in a
compile time error.
We can separate the
initialization from declaration. i.e
char str [5];
str = “Good”, is not
allowed.
Similarly
char s[4] = ‘abc’;
char z [4];
z=s; is not allowed.
An array name cannot be
used as the left operand of an assignment operator.
Reading strings
from terminal:
Using scanf Function:
The input function scanf can be used with %s format specification to
read a stirring of characters.
Ex: char add [10];
scanf ( “%s”,add);
In case of character
arrays , the (&) ampersand is not required before the variable name. The scanf function automatically
terminates the string that is read with a null character and the character
array should be large enough to hold the input string plus null character.
The problem with the
scanf function is that it terminates its input on the first white space it
finds. A white space includes blacks, tabs, new lines etc;
We can also specify the
field width using the form %ws in the scanf statement for reading a specified
number of characters from the input string.
Syntax: scanf (“%ws” name);
Here, 2 things may
happen.
1. The width w is equal
to or grater than the number of characters typed in. The entire string will
be stored in the string variables.
2. The width w is less
than the number of characters in the string. The excess characters will be truncated
and left unread.
Ex: char name [10];
scanf (“%5s”, name);
The
input RAM will be stored as:
The
input KRISHNA will be stores as:
Reading a line of text:
Scan with %s or %ws can
be read only strings without white spaces. i.e. they cannot used for reading
a text containing more than one word.
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 whitespaces
Ex : char line [80];
scanf (“%[L\n]”, line);
printf (“%s”, line);
Will read a line of input
from the keyboard & display the same on the screen.
|
|||||||||||||||||
Writing Strings to screen:-
Using printf function:
|
|||||||||||||||||
We can use the printf function
with %s format to print strings to the screen. The format %s can be used to
display an array of characters that is terminated by the null character.
Ex : printf (“%s”, name);
It can display the entire
content of the any name. we can also specify the precision (field width)
using a form %ws.
|
|||||||||||||||||
Ex : % 10.4
Indicates that the first
4 characters are to be printed in a field width of 10 columns.
|