Strings

Strings

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,

char name[20];

char address[25];

char city[15];

Initialization of strings

The string can be initialized as follows:

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

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

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

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

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:

char array-name[size][size];

For example,

char names[5][10];

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.

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

 

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

Drag and Drop Collection View cell in IOS 11 &... Drag and Drop Collection View cell in IOS 11 & swift 4 In this tutorial we are explain how to drag and drop collection view cell in two collectio...
New features in XCode 10 Supported Configurations Xcode 10 beta requires a Mac running macOS 10.13.4 or later Xcode 10 beta includes SDKs for iOS 12, watchOS 5, macOS ...
Introduction to JVM Introduction to JVM JVM stands for Java Virtual Machine. It works as an engine that derives the Java Code. The JVM does not understand Java ty...
E-Mail Format Formal Email / Letter Format A formal email is in general sent to someone you don’t know well or to someone who’s in authority. Examples of someone w...
UIPopOverPresentationController In Swift UIPopOverPresentationController UIPopOverViewController in swift is writen in swift 3.1 and Use the xcode Version 8.3.2. For use this import the Pop...
Invitation Letter for Visa Invitation Letter Samples These letter are necessary for taking visa of another country. so these samples are the most important document. Sample 1 ...

Leave a Reply

Your email address will not be published. Required fields are marked *