as

Wednesday, 12 November 2014

Built-in String Functions in C


In C there are 6 types of library functions as mentioned below.

String functions
  1. strcpy(s1,s2);        //Copies one string(s1) into another string(s1).
  2. strcat(s1,s2);        //Concatenates two strings.
  3. strlen(s1);             //Returns the length of string.
  4. strcmp(s1,s2);     //Compare two strings.
  5. strchr(s1,ch);
  6. strstr(s1,s2);

Below program compares 2 strings

#include<stdio.h>              

#include<string.h>

void main()
{
    char str1[15],str2[15];

    printf("Enter the first string\n");
    gets(str1);
    printf("Enter the second string\n");
    gets(str2);
    if( strcmp(str1,str2) == 0 )
     {
      printf("String is equal.\n");
     }

    else if( strcmp(str1,str2)< 0)
     {
      printf("Str1 is less than str2\n");
     }
    else
     {
      printf("Str1 is greater than str2\n");
     }
    getchar();
}


Below program concatenates 2 strings.

#include<stdio.h>             

#include<string.h>
void main()
{
   char str1[10];
   char str2[10];
      //accept the first string
     printf("Enter First string");
     scanf("%s",str1);
           
       //accept the second string
     printf("Enter second string");
     scanf("%s",str2);
           
      //concatenate string2 into string1
     strcat( str1, str2);
           
     printf("Concatenated string is: %s\n", str1 );

 getchar();

}


Below program will illustrate how we can format the string using printf function.
#include<stdio.h>


void main()
{
   //print whole string
  printf("*%s*\n", "Sagar Salunke");
     
    //Reserve 50 spaces for printing string...
      //if string is less than 50, oher characters will be blank spaces
      //- sign indicates that printing will be left justified
   printf("*%-50s*\n", "Sagar Salunke");

      //Reserve 50 spaces for printing string...
      //if string is less than 50, oher characters will be blank spaces
      //No sign indicates that printing will be right justified
    printf("*%50s*\n", "Sagar Salunke");

     //print 5 characters from string...
    printf("*%.5s*\n", "Sagar Salunke");

      //print 10 characters from string...
    printf("*%.10s*\n","Sagar Salunke");

      //Reserve 50 spaces for printing string...
      //but print only 10 characters from string...
  printf("*%50.10s*\n","Sagar Salunke");

      //Reserve 50 spaces for printing string...
      //but print only 10 characters from string right justified...
    printf("*%-50.10s*\n","Sagar Salunke");

 getchar();
}



Below program will append one string to another.


#include<stdio.h>
#include<string.h>
void main()

{
char str1[100];
char str2[20];
char str3[30];

int i=0,str1len,str2len;

printf("\nEnter first string -> ");
scanf("%s",str1);
printf("\nEnter second string -> ");
scanf("%s",str2);

//using built in function from string.h library
printf("\n Concatenated string is %s",strcat(str1,str2));

//using user defined logic.

//1. find the length of the both strings and then use for loop.

str1len = strlen(str1);
str2len = strlen(str2);

while(i < str2len)
{
str1[str1len+i] = str2[i];
i++;
}

str1[str1len+i] = '\0';

printf("\nWithout using strcat -> %s", str1 );


getchar();
}





Sponsored Links

Popular Posts

Comments

ShareThis

Google+ Followers

Google+ Badge