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

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");

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");

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:

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:

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:

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:

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

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


4.21.2013

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

Odd Number Triangle

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

1
1 3
1 3 5
1 3 5 7
1 3 5 7 9

Ans.

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

The output of above program would be:

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

4.18.2013

Simple Star Pyramid

Q. Write a C program to print the following star structure as:

*********
*******
*****
***
*

Ans.

/*c program for simple star pyramid*/
#include<stdio.h>
int main()
{
 int num=5,n=9,r,c;
 for(r=1; r<=num; r++,n=n-2)
 {
   for(c=1; c<=n; c++)
       printf("*");
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of simple star pyramid C program
Figure: Screen shot for simple star pyramid C program

4.17.2013

Star Pyramid

Q. print the following star structure as:

*****
****
***
**
*
**
***
****
*****

Ans.

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

The output of above program would be:

Output of star pyramid C program
Figure: Screen shot for star pyramid C program

Even Odd Number Star Pyramid

Q. Write a C program to print the following even odd number star pyramid as:

1
*2
1*3
*2*4
1*3*5
*2*4*6

Ans.

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

The output of above program would be:

Output of odd even number star pyramid C program
Figure: Screen shot for odd even number star pyramid C program

Number Star Pyarmid

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

1
1*
1*3
1*3*
1*3*5
1*3*5*

Ans.

/*number star c pyramid program*/
#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(c%2==0)
       printf("*");
    else
       printf("%d",c);
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

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

Reverse Number Pyramid

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

10 9 8 7
6  5 4
3  2
1

Ans.

/*c program for reverse number pyramid*/
#include<stdio.h>
int main()
{
 int n=4,num,r,c;
 static int p=10;
 num = n;
 for(r=1; r<=n; r++,num--)
 {
   for(c=1; c<=num; c++)
   {
     printf("%d ",p);
     p--;
   }
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output for reverse number pyramid C program
Figure: Screen shot for number pyramid C program

4.04.2013

Number star pyramid

Q. write a C program to print the following number star pyramid as:

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

Ans.

/* c program for number star pyramid */
#include<stdio.h>
int main()
{
 int num=6,r,c;

 for(r=1; r<=num; r++)
 {
   for(c=1; c<=r; c++)
   {
      if(r%2==0)
         printf("*");
      else
         printf("%d",c);
   }
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


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

4.01.2013

Square Star Pyramid

Q. Write a C program to print the square star pyramid design as:

 **** 
*    *
*    *
 ****

Ans.

/*c program for star square design*/
#include<stdio.h>
int main()
{
 int cols,rows,r,c,sp;
 printf("Enter no. of columns: ");
 scanf("%d", &cols);
 printf("Enter no. of rows: ");
 scanf("%d", &rows);
 for(r=1; r<=cols; r++)
 {
  if(r==1 || r==cols)
      printf(" ");
  else
      printf("*");
 }
 printf("\n");
 for(c=1; c<=rows-2; c++)
 {
   printf("*");
   for(sp=1; sp<=cols-2; sp++)
       printf(" ");
   printf("*\n");
 }
 for(r=1; r<=cols; r++)
 {
   if(r==1 || r==cols)
       printf(" ");
   else
       printf("*");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of Square Star Pyramid C program
Figure: Screen shot for Square Star Pyramid C program