C Programming

C Programming


strncmp()

Posted: 28 Apr 2013 11:08 AM PDT


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()

Posted: 28 Apr 2013 11:00 AM PDT

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()

Posted: 28 Apr 2013 10:51 AM PDT

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()

Posted: 28 Apr 2013 10:39 AM PDT

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()

Posted: 28 Apr 2013 10:22 AM PDT

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()

Posted: 28 Apr 2013 10:13 AM PDT

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()

Posted: 28 Apr 2013 03:02 AM PDT

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()

strlen()

Posted: 28 Apr 2013 02:31 AM PDT

This function counts the number of characters present in a string.
Hence, strlen() function return always numeric value.

syntax:

strlen("Any_string");

example:

strlen("C Program");

It return : 9

The following program illustrated the uses of strlen() function as:

/*c program to illustrate the use of strlen function*/
#include<stdio.h>
int main()
{
 int len;
 char str[40];
 printf("Enter any string : ");
 gets(str);
 len = strlen(str);
 printf("%s = %d Characters",str,len);
 getch();
 return 0;
}

The output of above program would be:


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

Related program:

  1. list of standard library string function
  2. strcpy()

C Standard Library Inbuilt Function

Posted: 28 Apr 2013 11:15 AM PDT


With every C compiler, a large set of useful string handling library function are provided.
Below lists the more commonly used functions along with their name and features as:


 Function name  Features
 strlen()  Calculate length of string
 strcpy()  Copy from one string to another
 strncpy()  Copies first n characters of one string into another
 strcat()  Appends one string at the end of another
 strncat()  Appends first n characters of a string at the end of another
 strcmp()  Compares two string
 strncmp()  Compares first n characters of two string
 strcmpi()  Compares two string with ignore the case
 stricmp  Compares two string with ignore the case(identical to strcmpi)
 strnicmp  Compares first n characters of two string with ignore the case
 strlwr  Convert a string to lowercase
 strupr  Convert a string to uppercase
 strrev  Reverse string
 strdup  Duplicate a string
 strchr  Finds first occurrence of a given character in a string
 strrchr  Finds last occurrence of a given character in a string
 strstr  Finds first occurrence of a given string in another string
 strset  Sets all characters of string to a given character
 strnset  Sets first n characters of a string to a given character
Table: Standard Library String Function in C Language

(For syntax, example and use of above function, click at related function name.)



Post a Comment