String in C Language

Raiyan Shahid
5 min readJun 11, 2021

--

String in C Language

In C there is no separate data type for string. String in C is treated as arrays of type char. Character in array is a string if it ends with null character, that null character is an escape sequence with ASCII value 0 and it is different from the ASCII character ‘0’. String is generally used to store and manipulate data in text form like words or sentences.

Constant in String (String Literal)

String constant is a sequence of characters enclosed in double quotes and is also called a literal. The double quotes are not a part of the string.

Example:

“v”

“Welcome To C Language”

“2456”

Whenever a string constant is written anywhere in a program, it is stored somewhere in memory as an array of characters terminated by a null character (‘\0’). The string constant itself becomes a pointer to the first character in the array .

Example :

String “Hello World” will be stored in array like this :

Hello WorlD\0

Each character occupies one byte and compiler automatilcally insert the null character at the end. String constant “Hello Word” is actually a pointer to the character. So whenever a string constant is used in the program it is replaces by a pointer pointing to the string.

If we have a pointer variable of type char *, we can assign the address of this string constant to it as

Char *p = ”Hello World”;

Similarly we can write:-

printf(“Hello Word”);

Identical string constant used in a program need not be distinct. It depends on the compiler whether they are stored separately or as same string that is they can be at same address or at different addresses.

So far we studied that a string constant gives the address of first character in it, but is an exception to this rule, when the string constant is used as an initializer for a character array then it does not represent array address and it is not stored anywhere in memory.

For example:

Char arr[]= “Hello”;

Here the string constant “Hello” is not stored in memory and thus does not represent any address. This declaration statement reserves space for array arr and is equivalent to :-

Char arr[]={‘H’, ’e’, ’l’, ’l’, ’o,’\0’};

String Variable

A string variable is a 1-d array of ASCII character terminated by a null character. To create a string variable we need to declare a character array with size sufficient to hold all the characters to the string including null character.

Example:

Char str[]= {‘D’, ’e’, ’l’, ’h’, ‘I’, ’\0’};

We may also initialize it as –

Char str[]=”Delhi”;

This initialization is same as the previous one and in this case the compiler automatically insert the null character at the end.

Example : Write a program to print a character of a string and address of each character?

#include <stdio.h>

int main(void)

{

Char str[]=”India”;

Int i;

For(i=0; str[i] != ’\0’;i++)

{

printf(“Character =%c\t”, str[i]);

printf(“Address =%p\n”, &str[i]);

}

return 0;

Output:

Character = I Address=0012FED0

Character = n Address=0012FED1

Character = d Address=0012FED2

Character = I Address=0012FED3

Character = a Address=0012FED4

String Library Function

In string there is several library function used to manipulates strings and the prototype for these functions are in header file string.h.

There are several string functions like strlen(), strcpy(), strcmp(), strcat() in Language.

strlen()

This function return the length of the string that is the number of characters in this string excluding the terminating null character.

Declaration : size_t strlen(char const *string);

The type size_t is defined in stddef.h and is an unsigned integer type. It accept a single argument, which is pointer to the first character of the string.

Example : strlen(“India”) returns the value 6.

Example: Write a Program in C to Demonstrate strlen() ?

#include<stdio.h>

#include<string.h>

int main(void)

{

char str[50];

printf(“Enter a string : “);

gets(str);

printf(“Length of the string is: %u\n”, strlen(str));

return 0;

}

Let us create this function using Array:

unsigned int astrlen(char str[])

{

Int i=0;

While(str[i]! = ‘\0’)

i++;

return i;

}

Output: Enter a string : CinDepth

Length of the string is :8

strcmp()

This function is used to lexicographic comparison of two string.

Declaration: int strcmp(const char *s1, const char *s2);

strcmp() returns a value 0, otherwise it returns a non-zero value. It compares the strings character by character and the comparison stops when either the end of string is reached or the corresponding characters in the two string are not same.

strcmp(s1,s2) return value :-

<0 when s1 < s2

=0 when s2 == s2

>0 when s1 > s2

Example: Write a Program in C to demonstrate strcmp() ?

#include<stdio.h>

#include<string.h>

Int main(void)

{

char str1[10], str2[10];

printf(“Enter two strings : “);

gets(str1);

gets(str2);

if((strcmp(str1,str2)) == 0)

printf(“String are same\n”);

else

printf(“String are not same\n”);

return 0;

}

Output:

Enter two strings : Bangalore

Mangalore

Strings are not same

Let us create this function using Array:

int astrcmp(char str1[], char str2[])

{

Int I;

For(i=0; str1[i] == str2[i]; i++)

If(str1[i] == ‘\0’)

return 0;

return(str1[i] — str2[i]);

}

strcpy()

This function is used for copying one string to another string.

Declaration : char *strcpy(char *s1, const char s2);

The call strcpy(str1,str2) copies str2 to st1 including the null character. Here str2 is the source string and str1 is the destination string. The old str1 including the null character. Here str2 is the source string and str1 is the destination string. The old contents of the destination string str1 are lost. The function returns a pointer to destination string str1.

The destination string should be a character array or a char pointer initialized to a character array or a char pointer initialized to dynamically allocated memory. It can’t be a string constant because it has to be modified. The function call like strcpy(“New”, str1) or strcpy(“New”, ”York”) are wrong because “New” is a string constant and so we can’t overwrite it.

Example : Write a Program in c to demonstrate strcpy() ?

#include<stdio.h>

#include<string.h>

int main(void)

{

char str1[10], str2[10];

printf(“Enter a string: ”);

scanf(“%s”,str2);

strcpy(str1, str2);

printf(“First String :%s \t\t second string : %s\n”, str1, str2);

strcpy(str1, ”Delhi”);

strcpy(str2, ”Bangalore”);

printf(“First String : %s \t\t Second String : %s\n”, str1,str2);

return 0;

}

Output:

Enter String Chennai

First String : Chennai Second String: Chennai

First String: Delhi Second String: Bangalore

Let us create this Function using Array:

char *astrcpy(char str1[], charstr2[])

{

Int i=0;

while((str1[i] = str2[i]) != ‘\0’)

i++;

return str1();

}

strcat()

This function is used to append a copy of a string at the end of other string.

Declaration: char *strcat(char *str1, const char *str2);

It first string is “Hello” and second “World” then after using this function the first string becomes “HelloWorld”. The null character from str1 is removed and str2 is added at the end of str1. The second string str2 remain unaffected. A pointer to the first string str1 is returned by the function. The result is undefined it both the strings overlap.

Example : Write a Program in C to Demonstrate strcat() ?

#include<stdio.h>

#include<string.h>

int main(void)

{

Char str1[20], str2[20];

printf(“Enter two strings : “);

gets(str1);

gets(str2);

strcat(str1, str2);

printf(“First String : %s\t Second string : %s\n”,str1,str2);

strcat(str1,”_one”);

printf(“Now first string is : %s\t String is : %s\n”,str1);

return 0;

}

Output:

Enter two string : data

Base

First String : database Second string : base

Now first String is : database_one

Let us create this Function using Array:

char *astrcat(char str1[], char str2[])

{

int i=0,j=0;

while(str1[i] != ‘\0’)

i++;

while(str1[i++] = str[j++])

;

return str1 ;

}

Refer — ArzaTechs

--

--