String in C Language

String in C

The string in C is defined as an array of characters ending with the special character (null character) “\ 0”. Therefore, the unfinished string contains characters that consist of a list preceded by null.

Syntax:

char string_name[size_str];

Where:

“String_name” name given to the string.

“Size_str” size of the string name.

Initialize String in C Language:

There are several ways to initialize a string in C. See the various examples below that show different ways to initialize a string in C.

// Different ways to initialize a string in C

char string_name[] = “mystring”;

char string_name[9] = “mystring”;

char string_name[] = {‘m’,’y’,’s’,’t’,’r’,’i’,’n’,’g’,’\0′};

char string_name[9] = {‘m’,’y’,’s’,’t’,’r’,’i’,’n’,’g’,’\0′};

All of the above methods map the string “mystring” to a string variable named string_name.

Rules and Regulations of String in C Language:

It is defined by double quotes, so defining a string with single quotes will generate an error.

Code:

char string_name[] = “mystring”;

Code:

char string_name[] = ‘mystring’;

You can use the scanf () or gets () functions to read a string from the user, and put () or printf () to display the string.

The following are commonly used string functions.

  • strcmp: This function is used to compare two strings.
  • strlen (): This function is used to measure length of the specified string.
  • strcpy (): This function is used to copy a string.
  • strcat (): This function is used to concatenate two strings.

For example:

Here Code:

#include <stdio.h>
#include <string.h>
int main()
{
	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));
	char str1[20]= "my string1"; 
	char str2[20]; 
	char str3[20]; 
	strcpy(str2, str1); 
	strcpy(str3, "string3"); 
	printf("vlaue of str1: = %s \n",str1); 
	printf("vlaue of str2: = %s \n",str2); 
	printf("vlaue of str3: = %s \n",str3); 
	char str_cmp1[20]= "my string1"; 
	char str_cmp2[20]= "my string1"; 
	char str_cmp3[20]= "my string 3"; 
	int result_compare = strcmp(str_cmp1, str_cmp2); 
	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");
	}
	char str_cat1[20]= "my string1"; 
	char str_cat2[20]= "my string2"; 
	strcat(str_cat1,str_cat2);
	printf("concatenated data of string is: = %s \n",str_cat1); 
	return 0;
}

Examples of String in C Language:

Example # 01:

Here Code:

#include<stdio.h> 
int main() 
{ 
	// declaring string 
	char str[50]; 
	// reading string 
	scanf("%s",str); 
	// print string 
	printf("%s",str); 
	return 0; 
}

Example # 02:

Here Code:

#include <stdio.h>
int main()
{
	char first_name[30]; 
	printf("Please Enter the first name of the person: "); 
	fgets(first_name, sizeof(first_name), stdin);  
	printf("The first name of the person is: ");
	puts(first_name);    
	
	char last_name[30]; 
	printf("Please Enter the last name of the person: "); 
	scanf("%s", last_name); 
	printf("The last name of the person is %s.", last_name); 
	return 0;
}

Passing strings to function:

Because a string is an array of strings, you can pass a string to a function in the same way that you pass an array to a function.

For example:
Here Code:
#include<stdio.h> 
void printStr(char str[]) 
{ 
	printf("String is : %s",str); 
} 
int main() 
{ 
	char str[] = "ProgrammersAcademy"; 
	printStr(str); 	
	return 0; 
}

Conclusion:

A string is a sequence of characters stored in an array of characters. Characters such as’d ‘are indicated by single quotes instead of strings. “C” provides standard library functions for manipulating strings in your program. The string handler is stored in the header file <string.h>. The string must be declared or initialized before it can be used programmatically. There are several input and output string functions, each with its own function. Don’t forget to include the string library to work with the function. You can use atoi (), atof (), and atol () to convert a string to a number. They are very useful for the encoding and decoding process. You can work with different strings by defining an array of strings in C.

Learn More : Function in C Language

References:

Leave a Reply

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