String

Notes Zone
0

 Strings 

A String in C programming is a sequence of characters terminated with a null character ‘\0’. The C String is stored as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character ‘\0’.


C String Declaration Syntax

Declaring a string in C is as simple as declaring a one-dimensional array. Below is the basic syntax for declaring a string.

char string_name[size];

In the above syntax string_name is any name given to the string variable and size is used to define the length of the string, i.e the number of characters strings will store.

There is an extra terminating character which is the Null character (‘\0’) used to indicate the termination of a string that differs strings from normal character arrays.


C String Initialization

A string in C can be initialized in different ways. We will explain this with the help of an example. Below are the examples to declare a string with the name str and initialize it with “GeeksforGeeks”.

We can initialize a C string in 4 different ways which are as follows:

1. Assigning a String Literal without Size

String literals can be assigned without size. Here, the name of the string str acts as a pointer because it is an array.

char str[] = "NotesZone";

2. Assigning a String Literal with a Predefined Size

String literals can be assigned with a predefined size. But we should always account for one extra space which will be assigned to the null character. If we want to store a string of size n then we should always declare a string with a size equal to or greater than n+1.

char str[50] = "NotesZone";

3. Assigning Character by Character with Size

We can also assign a string character by character. But we should remember to set the end character as ‘\0’ which is a null character.

char str[10] = { 'N','o','t','e','s','Z','o','n','e','\0'};

4. Assigning Character by Character without Size

We can assign character by character without size with the NULL character at the end. The size of the string is determined by the compiler automatically.

char str[] = { 'N','o','t','e','s','Z','o','n','\0'};


Strings and Pointers in C

In Arrays, the variable name points to the address of the first element.

Below is the memory representation of the string str = “Notes”.


Standard C Library – String.h  Functions

The C language comes bundled with <string.h>which contains some useful string-handling functions. Some of them are as follows:


strlen(string_name)         Returns the length of string name.

strcpy(s1,s2)                    Copies the contents of string s2 to string s1.

strcamp(str1,str2)            Compares the first string with the second string. If strings                                                               are the same it returns 0.

strcat(s1,s2)                    Concat s1 string with s2 string and the result is stored in the                                       first string.

strlwe()                             Converts string to lowercase.

strupr()                             Converts string to uppercase.

strstr(s1,s2)                      Find the first occurrence of s2 in s1





Tags

Post a Comment

0Comments

Post a Comment (0)