String in C (Standard Library Functions) – Set 4

In the last instructional exercise, I gave a concise presentation of the connection between strings and pointers. I additionally informed you concerning the 2D exhibit of characters.

Today I will inform you regarding some regularly utilized standard library string capacities. These capacities are made by the compiler sellers to accelerate the control of strings in C programming. So let’s begin.

Standard Library String Functions

There are in excess of 18 standard library string capacity however just 4 of them are utilized as often as possible. So I will just talk about those 4 capacities which are strlen(), strcpy(), strcat(), strcmp().

Note: The header record that we will use for these capacities is string.h.

strlen() in C

As its name proposes, this capacity is made to figure the length of the string. By utilizing this capacity we can compute the number of characters in any string.

#include<stdio.h>
#include<string.h>
 
int main() 
{  
	char name[]="JustTechReview";
	int x,y;
	x=strlen(name);
	y=strlen("Hello dear");
	printf("Lenth of first string %dnLength of second string %d",x,y);
	
	return 0;
}

Output

Lenth of first string 18 
length of second string 10 
Press any key to continue . . . 

Clarification

Here you can see I have determined the length of the string by two different ways. So you can either pass a string or string variable. Keep in mind while figuring the length it doesn’t check ” character.

strcpy() in C

This capacity is utilized to duplicate substance of one string to another. To utilize this capacity we simply need to pass the base location of the source and target string. Consider the underneath model.

#include<stdio.h>
#include<string.h>
 
int main() 
{  
	char source[]="Justtechreview";
	char target[20];
	strcpy(target,source);
	printf("Source string %snTarget String %sn",source,target);
	
	return 0;	
}

Output

Source string Justtechreview 
Target String Justtechreview 
Press any hey to continue . . . 

strcat() in C

This string capacity is utilized to connect (consolidate) two strings. To utilize this capacity we need to pass the base location of the two strings. Consider cautiously the underneath program code.

#include<stdio.h>
#include<string.h>
 
int main() 
{  
	char str1[50]="Hello ";
	char str2[20]="Justtechreview";
	strcat(str1,str2);
	printf("%sn",str1);
	
	return 0;
}

Output

Hello Justtechreview  Press any key to continue . 

strcmp() in C

Do you recall that I disclosed to you that we can’t analyze two strings legitimately simply like basic whole number factors?

This is on the grounds that it will consistently give the base location of the string to look at. Strcmp() work is utilized to analyze two strings.

This capacity returns 0 if the strings are indistinguishable and restore the distinction of ASCII esteems when a crisscross happens. Consider the beneath program cautiously.

#include<stdio.h>
#include<string.h>
 
int main() 
{  
	char source[]="Hello ";
	char target[]="Khello";
	int x,y;
	x=strcmp(source,target);
	y=strcmp(source,"Hello");
	printf("%dn%dn",x,y);
	
	return 0;
	
}

Output

-3
32
Press any key to continue......

Clarification

In the principal call, I have contrasted source string and target string that profits – 3 which is a numeric distinction of their confound ASCII characters. In the subsequent call, I have passed two indistinguishable strings. In this way, 0 is returned by the capacity.

DURGESH

I Love python, so I like machine learning a Lot and on the other hand, I like building apps and fun games I post blogs on my website for Tech enthusiast to learn and Share Information With The World.

Leave a Reply

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

7 − one =

Recent Content