iForeRunner
 
   
 
  c  
   
   
   
 
  LIBRARY FUCNTIONS IN C  
 
 



STRING OR LIBRARY FUNCTIONS

The following are some of the library functions available related to 'C' .

a) strlen():- This function is used to find the length of the string. It takes only one parameter and it is the string in double quotes or a variable containing a string. It returns integer value which is nothing but the no. of characters present in the variable.

syntax:
strlen(variable);
Eg:
main()
{
int l; char name[10]; clrscr(); printf("\nEnter a string :"); scanf("%s",name); l=strlen(name); printf("\nNumber of characters : %d",l); getch();
}
b) strcpy():- It is used to copy a string from one variable to another variable. It takes 2 parameters. First parameter is the destination variable into which you want to store and the second variable is the string that you want to copy. Here string1 is copied into string2.
Syntax:- strcpy( string2, string1);

Eg:
main()
{
char name[15],name1[15]; clrscr(); printf("\nEnter a string :"); scanf("%s",name); strcpy(name1,name); printf("\nString 1 is %s",name); printf("\nString 2 is %s",name1); getch();
}
c) Strcat():- This function is used to concatenate(join) 2 strings into a single string. It takes 2 parameters. The second parameter value will be joined to the first string.
Syntax:- strcat(string1, string2);
In the above syntax, string2 value will be joined to the end of string1.
Eg:
main()
{
char name[15],name1[35]; printf("\nEnter a string :"); scanf("%s",name); printf("\nEnter another string :"); scanf("%s",&name1); strcat(name1,name); printf("\nString after concatenation is %s",name); getch();
}

d) Strrev():- This function reverses the string value present in a string variable and after reverses stores the reversed string again in the same variable. It takes only one parameter and it is the string that you want to reverse.

Syntax:- strrev(string);
Eg:
								
main()
{
char name[15]; clrscr(); printf("\nEnter a string :"); scanf("%s",name); strrev(name); printf("\nReversed string is %s",name); getch(); }

e) Strcmp():- It is used to compare the 2 given strings. It takes 2 parameters. It returns value less than 0 when string1 is less than string2 and greater than 0 when string 1 is greater than string2. It returns 0 when both strings are equal. While comparing the 2 strings it will compare the ASCII codes for the characters in the string and returns the difference between the ASCII code values.

Syntax:- strcmp(string1, string2);
Eg:
								
main()
{
char name[10],name1[10]; int m ; clrscr(); printf("\nEnter a string :"); scanf("%s",name); printf("\nEnter another string :"); scanf("%s",name1); m=strcmp(name,name1); if(m==0) printf("\nBoth strings are equal"); else if(m<0) printf("\nString 1 is smaller"); else printf("\nString 2 is greater"); getch(); }
The above programs can also be done without using built in functions, by using for loops and arrays.

Please send comments to vgdarur.javafive@blogger.com