"Z Code Writer" is an Online Blog for Programmer & Beginner. Here, You will Find tech, Build your Programming Skills and other Programming Language Like C, C++, HTML, Java Script Java Code, Web Designing and University Of Sialkot (USKT) CGPA / GPA Calculator etc.

Thursday, 18 July 2019

String Variable

String Variable

      A string is a collection of characters. The characters may include alphabets, digits and special characters. A variable that is used to store a string is called string variable.
      The last character of every string variable is a null character. The null character is denoted by '\0'. This character marks the end of string. Thus when data is entered into a string variable, null character at the end of string will be added automatically. Thus if the string variable is declared with 10 characters, it can store only 9 characters.
      Suppose the size of a string variable is 12. The value "PAKISTAN" is assigned to it. The storage of this variable in memory is shown in the following figure.


P
A
K
I
S
T
A
N
‘\0’




Declaring String Variable

      Specifying the name of string variable and its length is called declaring string variable. The data type of string variable is 'char' type. The maximum numbers of characters that a string variable is to store is specified in square brackets in declaration statement. The syntax to declare a string type variable is as follows:
            char  string [ len ] ;

Where

string             It is the name of string variable.
len                  It indicates the size or length of string variable. It is an unsigned integer value.
For example, to declare the string variables 'name' and address' of length 25 and 35 respectively, the statement is written as;
              char  name [ 25 ] ,  address [ 35 ] ;


Initializing String Variable

      Like other variables, a string variable can also be initialized with a string at the time of its declaration. For example, to declare a string variable 'str' and to assign a string "PAKISTAN" into it, the statement is written as:
             char  str [ 14 ]  =  "PAKISTAN" ;
      The string "PAKISTAN" will be assigned to atring variable 'str' as shown below;

0
1
2
3
4
5
6
7
8
9
10
11
12
13
P
A
K
I
S
T
A
N
\0






      A string variable can also be declared and initialized with a string without specifying its length. For example, above statement can be written as;

                 char  str [ ]  =  "PAKISTAN" ;
      In the above statement, the string value 'str' is declared and "PAKISTAN" is assigned to it. The Null character (\0) will be added at the end of the string.


String Input and Output

      The gets() function and scanf() function can be used for input data into a string variable. Mostly, the gets() function is used for this purpose. Similarly, puts() function and printf() function are used to display the data of string variable on the screen. The format specifier "%s" with formatted input and output functions is used for input data into string and to display its data respectively.
      For example, to display the contents of "str" variable, the statement is written as;
                   printf ( "%s" ,  str ) ;


String Assignment

      Assigning a value to string variable is different than assigning values to other variables. The values are assigned to other variables using assignment statement. The assignment statement cannot be used to assign value to string variable. The following statement is invalid and compiler will generate an error:
            name  =     "I  Love  Pakistan" ;
      The string variable "name" does not represent to a single memory location. It is an array. An array consists of consecutive group of memory locations with the same name and data type. So different characters in different memory locations will be stored. One character of the string will be stored in one memory location.
C language provides built-in functions for processing strings. These functions are defined in header file "string.h". The string function strcpy() is used to copy or assign a string to a string variable. The general syntax this function is as follows:
                     strcpy ( strvar ,   string ) ;

Where

strvar           It indicates the string variable in which the string is to be copied/assigned.
string            It indicates the string to be copied/assigned.
For example to assign a string "I Love Pakistan" to string variable "name" , the statement is written as;
             strcpy ( name,   "I  Love  Pakistan" ) ;

No comments:

Post a Comment

Thanks For Visiting Here...!! I will Reply you as soon as possible.