Saturday, May 16, 2020

,

string funcation like strcmp, strcpy, strlen and more in c



Learn about string function in c programming and how to perform various operation on string. We will use pre-defined function to perform those operation on the string in c programming


string funcation like strcmp, strcpy, strlen and more in c
string function like strcmp, strcpy, strlen and more in c


You will learn about how to compare two stings (strcmp function), concatenate strings (strcat function), copy (strcpy function) and many more operations using a pre-define function in c programming lets start with function.


First Declare String in c programming : -  

Method 1 - 

char address[]={'T', 'E', 'X', 'A', 'S', '\0'};

Method 2 - 


char address[]="Function In Programming";

NULL character (\0)  automatically at the end of any string.


  • strlen String Function in c - 


size_t strlen(const char *str)

size_t represents unsigned short
strlen function uses to count the length of any string without end character in c programming.

Example:-

#include 
#include 
int main()
{
     char str1[20] = "Function in programming";
     printf("Length of string str1: %d", strlen(str1));
     return 0;
}

sizeof function in c programming returns the value of total allocated size which is assigned in the array.


  • strnlen String Function in c - 

Syntax for strnlen -

size_t strnlen(const char *str, size_t maxlen)

size_t represents unsigned short
This function returns the length of a string if it is less than the value specified for maxlen (maximum length) otherwise it returns maxlen value.

Example - 


#include 
#include 
int main()
{
     char str1[20] = "functioninc";
     printf("Length of string str1 when maxlen is 30: %d", strnlen(str1, 30));
     printf("Length of string str1 when maxlen is 10: %d", strnlen(str1, 10));
     return 0;
}

Output - 
String length is 13 when maxlen is 30
String length is 10 when maxlen is 10

In the second string maxlen is 10 so its gives 10.
  • strcmp String Function in c - 

strcmp function compare two string and give integer value. If both string are same then it gives 0 otherwise it may return negative and positive values depends on the comparison.

Example - 


#include 
#include 
int main()
{
     char s1[20] = "functioninprogramming";
     char s2[20] = "functioninc";
     if (strcmp(s1, s2) ==0)
     {
        printf("string 1 and string 2 are equal");
     }else
      {
         printf("string 1 and 2 are different");
      }
     return 0;
}

  • strncmp String Function in c - 

int strncmp(const char *str1, const char *str2, size_t n)

size_t represents unsigned short
strncmp function compare two string for first n character for both string.

Example - 


#include 
#include 
int main()
{
     char s1[20] = "program";
     char s2[20] = "program";
     /* below it is comparing first 8 characters of s1 and s2*/
     if (strncmp(s1, s2, 7) ==0)
     {
         printf("string 1 and string 2 are equal");
     }else
     {
         printf("string 1 and 2 are different");
     }
     return 0;
}

Output
Both strings are same


  • strcat String Function in c - 

Syntax 

char *strcat(char *str1, char *str2)

This function merge two string and return concatenated string.

Example - 


#include 
#include 
int main()
{
     char s1[10] = "Hello";
     char s2[10] = "World";
     strcat(s1,s2);
     printf("Output string after concatenation: %s", s1);
     return 0;
}

Output

HelloWorld

  • strncat String Function in c -

This function merges n character of two string and return concatenated string.

 Example -

#include 
#include 
int main()
{
     char s1[10] = "Hello";
     char s2[10] = "World";
     strncat(s1,s2, 3);
     printf("Concatenation using strncat: %s", s1);
     return 0;
}

OUTPUT - 

Hellowor ( Where n is 3)

  •  strcpy String Function in c -


char *strcpy( char *str1, char *str2)

This function use to copy string str2 in str1 with (terminator char /0)

 Example - 

#include 
#include 
int main()
{
     char s1[30] = "string 1";
     char s2[30] = "string 2 : I’m gonna copied into s1";
     /* this function has copied s2 into s1*/
     strcpy(s1,s2);
     printf("String s1 is: %s", s1);
     return 0;
}

OUTPUT : -

 String s1 is
string 2 : I’m gonna copied into s1

  •  strncpy String Function in c -

char *strncpy( char *str1, char *str2, size_t n)
size_t is unassigned n number.


This function has two cases: - 
Case 1 : -  str2 > n then it just copies n number of character in str1 from str2.
Case 2 : - str2<n then it copies all the character in str1 from str2 and appends several terminator chars(‘\0’) in empty space because n is greater the str2.

Example of strncpy: 

#include 
#include 
int main()
{
     char first[30] = "string 1";
     char second[30] = "string 2: I’m using strncpy now";
     /* this function has copied first 10 chars of s2 into s1*/
     strncpy(s1,s2, 12);
     printf("String s1 is: %s", s1);
     return 0;
}

OUTPUT : - 
 str1 is
I’m using st

  •  strchr String Function in c - 

This function used to search string str for character ch

 Example of strchr : -


#include 
#include 
int main()
{
     char mystr[30] = "this is it";
     printf ("%s", strchr(mystr, 'i'));
     return 0;
}

Output : -

strchr gives it from mystr for i.

  •  strrchr String Function in c -

This function is  similar to the function strchr, the only difference is that function strrchr will search a string in reverse order let see the example : -

#include 
#include 
int main()
{
     char mystr[30] = "this is it";
     printf ("%s", strrchr(mystr, 'f'));
     return 0;
}

Output : -

strchr gives it from mystr for i.

  •  strstr String Function in c -


char *strstr(char *str, char *srch_term)

strstr function is similar to the strchr, the only difference is that it searches for a string instead of single char like strchr.

Example for strstr : -  
 

#include 
#include 
int main()
{
     char inputstr[70] = "String in C at funcationinprogramming.com";
     printf ("Output string is: %s", strstr(inputstr, 'func'));
     return 0;
}

Output : - 

String is funcationinprogramming

These all are the string functions in c programming you can learn about them and make a use of them to save time in programming.

0 comments:

Post a Comment