String can be represented as a single-dimensional character type array. Strings in C are group of characters, digits, and symbols enclosed in quotation marks or simply we can say the string is declared as a “character array”. The end of the string is marked with a special character, the ‘\0’ (Null character), which has the decimal value 0. There is a difference between a character stored in memory and a Strings single character string stored in a memory. The character requires only one byte whereas the single character string requires two bytes (one byte for the character and other byte for the delimiter).

Declaration of strings

 

A string in C is simply a sequence of characters. To declare a string, specify the data type as char and place the number of characters in the array in square brackets after the string name. The syntax is shown as below:

char string-name[size];

For example,

[cc lang=”bash” escaped=”true” width=”auto” nowrap=”0″ theme=”blackboard” line_numbers=”off” ]

char name[20];

char address[25];

char city[15];

[/cc]

Initialization of strings

The string can be initialized as follows:

[cc lang=”bash” escaped=”true” width=”auto” nowrap=”0″ theme=”blackboard” line_numbers=”off” ]

char name[ 8] = {‘P’, ‘R’, ‘O’, ‘G’, ‘R’, ‘A’, ‘M’, ‘\0’};

[/cc]

The C compiler inserts the NULL (\0) character automatically at the end of the string. So initialization of the NULL character is not essential.

[cc lang=”bash” escaped=”true” width=”auto” nowrap=”0″ theme=”blackboard” line_numbers=”off” ]

char str[4] = {‘u’, ‘n’, ‘i’, ‘x’};

char str[5] = {‘u’, ‘n’, ‘i’, ‘x’, ‘\0’};

[/cc]

Array Of Strings

 

Array of strings are multiple strings, stored in the form of table. Declaring array of strings is same as strings, except it will have additional dimension to store the number of strings. Syntax is as follows:

[cc lang=”bash” escaped=”true” width=”auto” nowrap=”0″ theme=”blackboard” line_numbers=”off” ]

char array-name[size][size];

[/cc]

For example,

[cc lang=”bash” escaped=”true” width=”auto” nowrap=”0″ theme=”blackboard” line_numbers=”off” ]

char names[5][10];

[/cc]

where names is the name of the character array and the constant in first square brackets will gives number of string we are going to store, and the value in second square bracket will gives the maximum length of the string.

[cc lang=”bash” escaped=”true” width=”auto” nowrap=”0″ theme=”blackboard” line_numbers=”off” ]

char names [3][10] = {“Johns”, “Marks”, “Sumit”};

[/cc]

 

Standard Library String Functions

With every C compiler a large set of useful string handling library functions are provided in string.h file.

  • strlen – Finds length of a string
  • strcpy – Copies a string into another
  • strncpy – Copies first n characters of one string into another
  • strcmp – Compares two strings
  • strlwr – Converts a string to lowercase
  • strupr – Converts a string to uppercase
  • strcat – Appends one string at the end of another
  • strncat – Appends first n characters of a string at the end of another
  • strncmp – Compares first n characters of two strings
  • strcmpi – Compares two strings without regard to case (“i” denotes that this function ignores case)
  • stricmp – Compares two strings without regard to case (identical to strcmpi)
  • strnicmp – Compares first n characters of two strings without regard to case
  • strdup – Duplicates a string
  • strchr – Finds first occurrence of a given character in a string
  • strrchr – Finds last occurrence of a given character in a string
  • strstr – Finds first occurrence of a given string in another string
  • strset – Sets all characters of string to a given character
  • strnset – Sets first n characters of a string to a given character
  • strrev – Reverses string
  • strtok – Tokenizing given string using delimiter

Related Post

Compile C/C++ program (Dev-C/C++) what is Dev-C/C++? Dev-C/C++, developed by Bloodshed Software, is a fully featured graphical IDE(Integrated Development Environment),which ...
Loop Control Statement Loop control statements are used when a section of code may either be executed a fixed number of times. C gives you a choice of three types of loop st...
Getting started with C Getting started with C : If we want to communicate with our computer it is very much needed that our computer understands what we want to say to it. ...
Continue Statement in JAVA Continue Statement in JAVA Suppose you are working with loops. Sometime it is desirable terminate the loop or skip some statement inside the loop with...
First Java Program First Java Program NOTE: You need to have java installed on your system. Step 1: Open a text editor, like Notepad on windows and TextEdit on Mac. W...
New Tools for 64-bit Support in Xcode 9.3 New Tools for 64-bit Support in Xcode 9.3 The last macOS release to support 32-bit apps without compromise is macOS High Sierra. Make sure future rele...
Author

Write A Comment