Initialize & Implement of String in C

Initialize & Implement of String in C
Techiio-author
Written by Shuvhojit DebFebruary 3, 2022
9 min read
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this tutorial, you will learn the initialization and implementation of String in C.

What is String in C?

A String in C is nothing but a collection of characters in a linear sequence. ‘C’ always treats a string as a single data even though it contains whitespaces. A single character is defined using a single quote representation. A string is represented using double quote marks.

Example

"Welcome to the world of programming!"

‘C’ provides a standard library <string.h> that contains many functions which can be used to perform complicated operations easily on Strings in C.

How to Initialize a String in C?

Let’s study the String initialization in C. The following example demonstrates the initialization of Strings in C,

char first_name[15] = "ANTHONY";
char first_name[15] = {'A','N','T','H','O','N','Y','\0'}; // NULL character '\0' is required at end in this declaration
char string1 [6] = "hello";/* string size = 'h'+'e'+'l'+'l'+'o'+"NULL" = 6 */
char string2 [ ] = "world";  /* string size = 'w'+'o'+'r'+'l'+'d'+"NULL" = 6 */
char string3[6] = {'h', 'e', 'l', 'l', 'o', '\0'} ; /*Declaration as set of characters ,Size 6*/
In string3, the NULL character must be added explicitly, and the characters are enclosed in single quotation marks.

‘C’ also allows us to initialize a string variable without defining the size of the character array. It can be done in the following way,

char first_name[ ] = "NATHAN";

The name of Strings in C acts as a pointer because it is an array.

Examples to Implement String in C

The example to implement string in C is given below:

Example #1

Code:

// Example code to explain valid string declaration using double quotes

// include all required header file

#include <stdio.h>
// main function
int main()
{
// Body of main function
char name1[] = "my example string1"; // string name1 is defined using double quotes which is valid
char name2[] = 'my example string2'; // string name2 is defined using single quotes which is not valid This will throw an error
return 0;
}

Output:

blogpost

Example #2

Code:

// Example code to explain valid string declaration using double quotes

// include all required header file

#include <stdio.h>
// main function
int main()
{
// Body of main function
// Example of reading a string using fgets and displaying string using puts
char first_name[30]; // declaration of first_name string
printf("Please Enter the first name of the person: "); // Asking for first name from the user
fgets(first_name, sizeof(first_name), stdin);  // reading input string from the user using fgets function
printf("The first name of the person is: ");
puts(first_name);    // displaying string using puts function
// Example of reading a string using fgets and displaying string using puts
char last_name[30]; // declaration of last_name string
printf("Please Enter the last name of the person: "); // Asking for first name from the user
scanf("%s", last_name); // reading input string from the user using scanf function
printf("The last name of the person is %s.", last_name); // displaying string using printf function
return 0;
}

Output:

blogpost

Example #3

Code:

// Example code to understand string functions in C

// include all required header files

#include <stdio.h>
#include <string.h> // this header file contains string functions
// main function
int main()
{
// Body of main function
// Example to calculate length of the string
char string1[20]="my string1";
char string2[20]="hello";
printf("The calculated length of string1 is : = %ld \n",strlen(string1));
printf("The calculated length of string2 is : = %ld \n",strlen(string2));
// Example to copy a string
char str1[20]= "my string1"; // declaration of string str1
char str2[20]; // declaration of string str2
char str3[20]; // declaration of string str3
strcpy(str2, str1); // copying str data to str2
strcpy(str3, "string3"); // copying "string3" to str3
printf("vlaue of str1: = %s \n",str1); // displaying the value of str1
printf("vlaue of str2: = %s \n",str2); // displaying the value of str2
printf("vlaue of str3: = %s \n",str3); // displaying the value of str3
// Example to compare strings
char str_cmp1[20]= "my string1"; // declaration of string str_cmp1
char str_cmp2[20]= "my string1"; // declaration of string str_cmp2
char str_cmp3[20]= "my string 3"; // declaration of string str_cmp3
int result_compare = strcmp(str_cmp1, str_cmp2); // if two strings are identical it will return 0
if(result_compare == 0)
{
printf("str_cmp1 and str_cmp2 are identical string \n");
}
else
{
printf("str_cmp1 and str_cmp2 are not identical string \n");
}
int result_compare2 = strcmp(str_cmp1, str_cmp3);
if(result_compare2 == 0)
{
printf("str_cmp1 and str_cmp3 are identical string \n");
}
else
{
printf("str_cmp1 and str_cmp3 are not identical string \n");
}
// Example to concatenate two strings
char str_cat1[20]= "my string1"; // declaration of string str_cat1
char str_cat2[20]= "my string2"; // declaration of string str_cat2
//concatenates str_cat1 and str_cat2 and resultant string is stored in str_cat1.
strcat(str_cat1,str_cat2);
// display the concatenated string
printf("concatenated data of string is: = %s \n",str_cat1); // displaying the value of str_cat1
return 0;
}

Output:

blogpost

Conclusion

The ideas of string declaration, string initialization, and other string-related concepts in C were covered in this tutorial. These ideas come in handy when working with them.

C Language
C String
Program
String Implement
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance