Sunday, 28 April 2013

strncmp()


This function compare n character in two strings to find out whether they are same or different.

The two string are compared character by character until there is a mismatch or end of one of the strings is reached, whichever occurs first.

If the two strings are identical, strncmp() return zero.
If they are not, it returns the numeric difference between the ASCII values of the first non-matching pair of characters.

In summarize:
(Compare ASCII values of string's characters)
if both string equal = 0
if first string > second string = +ve
if first string < second string = -ve

Note: strncmp() is case sensitive.

syntax:

strncmp("first_sting","second_string",no_of_character);


example:

strncmp("ABZ","ABC",2);
[Here, There are only first 2 characters are compare.
First string's first character(i.e. A) is ASCII value is 65 and in second string's first character(i.e. B) ASCII value is 66 so first iteration result is 0, now second iteration start, now B and B (i.e. 66 and 66] are comparing so B==B (i.e. 66==66) so result is zero.]

illustrate strncmp() C program:

/*c program for illustrate strncmp() function*/
#include<stdio.h>
int main()
{
 int r;
 char str1[40],str2[40];
 printf("Enter first string : ");
 gets(str1);
 printf("Enter second string : ");
 gets(str2);
 r = strncmp(str1,str2,2);
 printf("Result : %d",r);
 getch();
 return 0;
}

The output of above program would be:


Output for strncmp() function uses in C program
Figure: Screen shot for strncmp() function uses in C program

Related program:

  1. List of standard library function

strcmpi()

This is function is same as strcmpi() aspect it is not case sensitive. 

This function compare two strings to find out whether they are same or different.

The two string are compared character by character until there is a mismatch or end of one of the strings is reached, whichever occurs first.

If the two strings are identical, strcmpi() return zero.
If they are not, it returns the numeric difference between the ASCII values of the first non-matching pair of characters.

In summarize:
(Compare ASCII values of string's characters)
if both string equal = 0
if first string > second string = +ve
if first string < second string = -ve

syntax:

strcmpi("first_sting","second_string");

example:

strcmpi("ABC","abc");
[Here, 
first comparison: A==a
second comparison: B==b
Thired comparison: C==c
So result = 0]

illustrate strcmpi() C program:

/*c program for illustrate strcmpi() function*/
#include<stdio.h>
int main()
{
 int r;
 char str1[40],str2[40];
 printf("Enter first string : ");
 gets(str1);
 printf("Enter second string : ");
 gets(str2);
 r = strcmpi(str1,str2);
 printf("Result : %d",r);
 getch();
 return 0;
}

The output of above program would be:


output for strcmpi() function uses in C program
Figure: Screen shot for strcmpi() function uses in C program


Related program:

  1. List of standard library function

strcmp()

This function compare two strings to find out whether they are same or different.

The two string are compared character by character until there is a mismatch or end of one of the strings is reached, whichever occurs first.

If the two strings are identical, strcmp() return zero.
If they are not, it returns the numeric difference between the ASCII values of the first non-matching pair of characters.

In summarize:
(Compare ASCII values of string's characters)
if both string equal = 0
if first string > second string = +ve
if first string < second string = -ve

Note: strcmp() is case sensitive.

syntax:

strcmp("first_sting","second_string");


example:

strcmp("AA","BB");
[Here, first string's first character(i.e. A) is ASCII value is 65 and in second string's first character(i.e. B) ASCII value is 66 so first iteration result is 0, now second iteration start now compare A and B (i.e. 65 and 66] so A<B (i.e. 65<66) so result is negative.]

illustrate strcmp() C program:

/*c program for illustrate strcmp() function*/
#include<stdio.h>
int main()
{
 int r;
 char str1[40],str2[40];
 printf("Enter first string : ");
 gets(str1);
 printf("Enter second string : ");
 gets(str2);
 r = strcmp(str1,str2);
 printf("Result : %d",r);
 getch();
 return 0;
}

The output of above program would be:


Output for strcmp() when both string are equal C program
Figure: Screen shot for strcmp() when both string are equal C program


Output for strcmp() when first string < second string  C program
Figure: Screen shot for strcmp() when first string < second string
 
C program
output for strcmp() when first string > second string  C program
Figure: Screen shot for strcmp() when first string > second string
 C program


Related program:
  1. List of standard library function
  2. strlen()
  3. strcpy()

strncpy()

This function copy only first n characters of source string to target string.
n = no. of characters(i.e. numeric value)

syntax:

strncpy("target_string","Source_string",no_of_characters);

example:

strncpy(target,source,3);
Output: souget

illustrate uses of strncpy() in C program:


/*c program to illustrate strncpy() function*/
#include<stdio.h>
int main()
{
 char source[40],target[40]="target";
 printf("Enter any string : ");
 gets(source);
 strncpy(target,source,3);
 printf("\nTarget:%s \nSource:%s",target,source);
 getch();
 return 0;
}

The output of above program would be:


Output of strncpy() function uses in C program
Figure: Screen shot for illustrate strncpy() function C program


Note: strncpy() function not copy the 'NULL character'( i.e. '\0').


Related program:
  1. List of standard library function
  2. strlen()
  3. strcpy()

strncat()

This function concatenates n characters of source string in the target string.


syntax:

strncat("target_string","source_string",no_of_character);

example:

source="book"
target="face"
strncat(target,source,3);
[Here, source string(i.e. book) first 3 characters is add at the end of target string(i.e. face). Hence now value of target string(i.e. face) is "faceboo".

illustrate strcat() in C program:


/*c program for strncat() function illustrate*/
#include<stdio.h>
int main()
{
 char source[40],target[40];
 printf("Enter source string : ");
 gets(source);
 printf("Enter target string : ");
 gets(target);
 strncat(target,source,3);
 printf("\nTarget:%s \nSource:%s",target,source);
 getch();
 return 0;
}

The output of above program would be:


Output of strncat() function used in C program
Figure: Screen shot for illustrate strncat() function C program


Related program:
  1. List of standard library function
  2. strlen()
  3. strcpy()
  4. strncpy()
  5. strcat()

strcat()

This function concatenates the source string at the end of the target string.

syntax:

strcat("target_string","source_string");

example:

source="book"
target="face"
strcat(target,source);
[Here, source string(i.e. book) is add at the end of target string(i.e. face). Hence now value of target string(i.e. face) is "facebook".

illustrate strcat() in C program:


/*c program for strcat() function illustrate*/
#include<stdio.h>
int main()
{
 char source[40],target[40];
 printf("Enter source string : ");
 gets(source);
 printf("Enter target string : ");
 gets(target);
 strcat(target,source);
 printf("\nTarget:%s \nSource:%s",target,source);
 getch();
 return 0;
}

The output of above program would be:


Output of illustrate strcat() function C program
Figure: Screen shot for illustrate strcat() function C program


Related program:
  1. List of standard library function
  2. strlen()
  3. strcpy()
  4. strncpy()

strcpy()

This function copies the contents of one string into another.
The base address of the source and target strings should be supplied to this function.

Note: If target_string is not empty and it is uses in strcpy() function, then its overlap with source_string.

syntax:

strcpy("target_string","source_string");

example:

strcpy(target_string,source_string);

illustrate uses strcpy() in C program:


/c program for illustrate of strcpy() function*/
#include<stdio.h>
int main()
{
 char source[40],target[40];

 printf("Enter any string : ");
 gets(source);
 strcpy(target,source);
 printf("Target %s=Source %s",target,source);
 getch();
 return 0;
}

The output of above program would be:


Output of strcpy() function uses in C program
Figure: Screen shot of strcpy() uses in C program

Related Article:

  1. List of standard library function
  2. strlen()