Sunday, April 28, 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()

strlen()

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

Saturday, April 27, 2013

C Standard Library Inbuilt Function


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

Sunday, April 21, 2013

Number Character Triangle

Q. Write a C program to print the following number character pyramid as:

1
21A
321AB
4321ABC
54321ABCD

Ans.

/*c program for number character triangle*/
#include<stdio.h>
int main()
{
 int num,r,c,p,z;

 printf("Enetr no. of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(c=r; c>=1; c--)
      printf("%d",c);
  for(z='A',p=r; p>1; p--,z++)
      printf("%c",z);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of number character pyramid C program
Figure: Screen shot for number character pyramid C program


Odd Number Triangle

Q. Write a C program to print the following number triangle as:

 0
 1 0 1
 2 1 0 2 1
 3 2 1 0 1 2 3

Ans.

/*c program for odd number triangle*/
#include<stdio.h>
int main()
{
 int num,r,c,q,s;
 printf("Enter ending number : ");
 scanf("%d", &num);
 for(r=0; r<=num; r++)
 {
  for(c=0,s=r; c<=r; c++,s--)
     printf(" %d",s);
  for(q=1; q<=r; q++)
     printf(" %d",q);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of number triangle C program
Figure: Screen shot for number triangle C program

Continue Character-Number Pyramid

Q. Write a C program to print the following character number pyramid as:

1
A B
2 3 4
C D E F
5 6 7 8 9

Ans.

/*c program character number pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c;
 static int i=1;
 static char ch='A';
 printf("Enter no. of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(c=1; c<=r; c++)
  {
    if(r%2==0)
       printf(" %c",ch++);
    else
       printf(" %d",i++);
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of character number pyramid C program
Figure: Screen shot for character-number pyramid C program

Differ Symbol Pyramid

Q. write C program to print the following symbol pyramid as:

*
$$
***
$$$$

Ans.

/*c program for symbol pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c;
 printf("Enter no. of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
   for(c=1; c<=r; c++)
   {
     if(r%2==0)
        printf(" $");
     else
        printf(" *");
   }
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of differ symbol pyramid C program
Figure: Screen shot for differ-symbol pyramid C program

Character - Symbol Pyramid

Q. Write a C program to print the following character and symbol design:

A
**
ABC
****
ABCDE
****
ABC
**
A

Ans.

/*c program for character and star pyramid*/
#include<stdio.h>
int main()
{
 char ch,r,c,q;
 printf("Enter ending character : ");
 scanf("%c", &ch);
 if(ch>='a' && ch<='z')
    ch=ch-32;
 for(r='A'; r<=ch; r++)
 {
  for(c='A'; c<=r; c++)
  {
    if(r%2==0)
       printf("*");
    else
       printf("%c",c);
  }
  printf("\n");
 }
 for(q=ch-1; q>='A'; q--)
 {
  for(c='A'; c<=q; c++)
  {
    if(q%2==0)
       printf("*");
    else
       printf("%c",c);
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:
Output of character symbol pyramid C program
Figure: screen shot for character star pyramid C program

-------------------------------------------


Q. Write a C program to print the following number and symbol design:

1
**
123
****
12345
****
123
**
1

Ans.

/*c program for number and star pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c,n;
 printf("Enter ending number : ");
 scanf("%d"&num);
 for(r=1; r<=num; r++)
 {
  for(c=1; c<=r; c++)
  {
    if(r%2==0)
       printf("*");
    else
       printf("%d",c);
  }
  printf("\n");
 }
 for(n=num-1; n>=1; n--)
 {
  for(c=1; c<=n; c++)
  {
    if(n%2==0)
       printf("*");
    else
       printf("%d",c);
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of number symbol pyramid C program
Figure: Screen shot for number symbol pyramid C program