c strings tutorial

Bodhak c strings tutorial in this topic we will cover in detail about What is String, Declaring Strings, Init Strings, String Operations, Working with multiple strings with syntaxes and examples.

What is String: A group of characters enclosed in double quotes is known as a string. Technically a string is nothing but a character array of a single dimension, whose index starts with zero. Every string implicitly terminated with a special character called NULL or ‘\0’.



c strings tutorial

One can write the own code to manipulate a string or can use standard library functions provided by most of the ‘C’ compilers.

Character strings are often used to build meaningful and readable programs. The common operations performed on character strings are:

* Reading and writing strings

* Combining strings together

* Copying one string to another

* Comparing strings for equality

* Extracting a portion of a string

Declaring Strings:

The general form of declaration of a string variable would be,

char string name[size];

where size indicates the number of characters in the character array.

When a string is assigned to a character array, it is automatically supplied a null character (‘\0’) at the end of the string by the compiler. Character arrays may be initialized when they are declared. And the size of the array should be equal to the maximum number of characters in the string plus one
Reading a line of Text:

In many text-processing applications, we need to read in an entire line of text from the terminal. It is not possible to use scanf() function to read a line containing more than one word. This is because the scanf() terminates reading as soon as space is encountered in the input.

To overcome above disadvantage C provides a string input function called gets( ) which will not treat single or multiple blank spaces as the end of the string till we are going to give newline character (\n).

The equivalent output function for gets( ) is puts() , which is used to display the strings on to the output screen.

char name[20];

gets (name);