Strings In C Programming Language

String:- A string in C is a list of characters that ends with a null character (\0).
There is no data type available to create strings in C language. To store string in C language, you create a character array. Therefore, the character array in C is also called string. The strings in C language terminate with null character (\0).
String
General syntex of string is:

In C, you can declare and initialize strings in two ways:
First Method:- You create an array of a finite number and store one character of string in each of its indexes. A null character (\0) is stored in the last index of this array. It is necessary to do this.

Second Method:- You can create an undefined char array and assign it a string. The array automatically becomes the same size as the string size. In this type of initialization, null character (\0) is automatically added.
Read Also:
  1. File Handling
  2. Memory Allocation
  3. Data Structures
This method is very easy way to create string but it can never be changed.

A string can also be declared as a set of characters.
For example:

Declaring and initializing strings:- A string variable is any valid C variable name and is always declared as an array.
char string name [size];
size determines number of characters in the string name. When the compiler assigns a character string to a character array, it automatically supplies a null character (‘\0’) at end of String. Therefore, size should be equal to maximum number of character in String plus one.
String can be initialized when declared as
1. char city*10+= “NEW YORK’;
2. char city[10]= ,‘N’,’E’,’W’,’ ‘,’Y’,’O’,’R’,’K’,’/0’-;
3.C also permits us to initializing a String without specifying size.
Ex:- char Strings* += ,‘G’,’O’,’O’,’D’,’\0’-;
String handling functions:- There are large numbers of string handling functions that can be used to carry out many of the string manipulations. These functions are packaged in string.h library. 
The following are the most commonly used string handling functions. 
strcat() : It is used to concatenate(combine) two strings.
strlen() : It is used to show length of a string.
strrev() : It is used to show reverse of a string. 
strcpy() : It is used to copies one string into another.
strcmp() : It is used to compare two string.
gets() And puts() functions : If you want to read from string user at run time then you can use the get() function for this. This function is defined for this purpose. In this function you pass the name of the char array in which you want to store the string. 
The general structure of this function is given below. 

Any string you pass is stored in the given char array. Now if you print that string from this array, then you have to use the loop. But you do not need to do this. C language provides you with the puts() function to print a char array as a complete string. In this function, the char array is passed which you want to print as complete string. 
It's general structure is given below.  

Post a Comment

1 Comments