Monday, 9 April 2018


A string is a sequence of characters. Any sequence or set of characters defined within double quotation symbols is a constant string. In C it is required to do some meaningful operations on strings they are :
* Reading string displaying strings.
* Combining or concatenating strings.
* Comparing string and checking whether they are equal.
* Extraction of a portion of a string.

Strings are stored in memory as ASCII codes of characters that make up the string appended with '\0' (ASCII value of null). Normally each character is stored in one byte, successive character are stored in successive bytes.

The last character is the null character having ASCII value zero.

Initializing Strings

  Following the discussion on character arrays, the initialization of a string must the following form which is simpler to one dimension array.
  char month1[ ] = { 'j' , 'a' , 'n' , 'u' , 'a' , 'r' , 'y'};
Then the string month is initializing to January. This is perfectly valid but C offers a special way to initialize strings. The above string can be initialized char month1 [ ]="January"; . The characters of the string are enclosed within a part of double quotes. The compiler takes care of string enclosed within a pair of a double quotes. The compiler takes care of storing the ASCII codes of characters of the string in the memory and also stores the null terminator in the end.

/* String.c string variable */
main( )
char month[15];
printf("Enter the string");
printf("The string entered is %s",month);

In this example string is stored in the character variable month. The string is displayed in the statement.
printf("The string entered is %s",month);

It is one dimension array. Each character occupies a byte. A null character(\0) that has the ASCII value 0 terminates the string. The table shows the storage of string January in the memory recall that \0 specifies a single character whose ASCII  value is zero.

Character string terminated by a null character '\0'.

A string variable is any valid C variable name and is always declared as an array. The general form of declaration of a string variable is
  Char string_name[size];
The size determines the number of characters in the string name.

Example :
  char month[10];
  char address[100];
The size of the array should be one byte more than the actual space occupied by the string since the compiler appends a null character at the end of the string.

Reading String from the Terminal
  The function scanf with %s format specification is needed to read the character string from the terminal.

Example :
 Char address[15];

Scanf statement has a drawback it just terminates the statement as soon as it finds a blank space , suppose if we type the string new york then only the string "new" will be read and since there is a blank space after word "new" it will terminate the string.

Note that we can use the scanf( ) without the ampersand symbol before the variable name. In many applications it is required to process text by reading an entire line of next from the terminal.

The function getchar( ) can be used repeatedly to read a sequence of successive single characters and store it in the array.

We cannot manipulate strings since C does not provide any operators for string. For instance we cannot assign one string to another directly.

For example :
Are not valid. To copy the chars in one string to another string we may do so on a character to character basis.

Writing String to Screen
  The printf statement along with format specifier %s to print strings on to the screen. The format %s can be used to display an array of characters that is terminated by the null character. For example : printf("%s",name); can be used to display the entire contents of the array name.

Arithmetic Operations on Characters
 We can also manipulate the characters as we manipulate numbers in C language. Whenever the system encounters the character data it is automatically converted into an integer value by the system. We can represent a character as an interface by using the following method.
Arithmetic operations can also be performed on characters. For example x='z'-1; is a valid statement. The ASCII value of 'z' is 122 the statement therefore will assign 121 to variable x.

It is also possible to use character constant in relational expressions. For example : ch>'a' && ch <='z' will check whether the character stored in variable ch is a lower case letter.


Post a Comment

Popular Posts


Android (21) AngularJS (1) Books (3) C (75) C++ (81) Data Strucures (4) Engineering (13) FPL (17) HTML&CSS (38) IS (25) Java (85) PHP (20) Python (85) R (69) Selenium Webdriver (2) Software (13) SQL (27)