Thursday, 1 February 2018

What is Strings in C language?

Welcome back! In my last post, I tell about pointers in C after that I want to tell you about String in  C that is used to suggest strings in which the stored data does not represent text. So let's start...

Definition and Concepts:

  • A string is a sequence of symbols that are chosen from a set of alphabet. A string is, essentially, a sequence of characters.
  • A string is generally understood as a data type storing a sequence of data values, usually bytes, in which elements usually stand for characters according to a character encoding, which differentiates it from the more general array data type.
  • When a string appears literally in source code, it is known as a string literal and has a representation that denotes it as such.
       For example,
       char unit;
       unit='F';       /*for Fahrenheit*/
       ...........
       unit='C';      /*for Centigrade*/
  • Most codes and ID's require more than one character. In mailing addresses, two characters are used to identify a state and five characters are used to identify a zip code. They could be stored in character array as follows:
       char state[2], zip[5];

Declaration of Strings:

In C, a character array is declared using the basic data type char and the derived data type array.

Syntax:  char StringName[array_size];

For example, char chararray[n];

The data type here is char, the name of the array is chararray, and n is the number of elements.

Standard Library Functions:

The most commonly used functions in the string library are:

strlen()
gets string length
strcat()
Concatenate two strings
strncat()
Concatenate one string with part of another
strcpy()
Copy a string
strcmp()
Compare two strings
strrev()
Reverse the string
strlwr()
Changes the string from upper to lowercase
strupr()
Changes the string from lower to uppercase
strstr()
Finds location of first substring in the string

0 comments:

Post a Comment