Monday, 9 April 2018

Strings

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 */
#include<stdio.h>
main( )
{
char month[15];
printf("Enter the string");
gets(month);
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("%s",address);

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 :
 String="xyz";
String1=string2;
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.
                 X='a';
               printf("%d\n",x);
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.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (51) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (931) Python Coding Challenge (358) Python Quiz (22) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses