Tuesday, January 11, 2011

C Program to Sorting a Strings Using Arrays with Built-In Functions

Coding:


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
  int i,j,n;
  char str[20][20],temp[20];
  clrscr();
  printf("Enter the no. of string to be sorted:");
  scanf("%d",&n);
  for(i=0;i<=n;i++)
      gets(str[i]);
  for(i=0;i<=n;i++)
      for(j=i+1;j<=n;j++){
  if(strcmp(str[i],str[j])>0){
      strcpy(temp,str[i]);
     strcpy(str[i],str[j]);
     strcpy(str[j],temp);
  }
      }
  printf("\nThe sorted string\n");
  for(i=0;i<=n;i++)
      puts(str[i]);
 getch();
}


Output:


C Program to Sorting a Strings in Case Sensitive Order

Coding:


#include<stdio.h>
#include<string.h>
#include<conio.h>
int comparison(char *s,char *t);

#define MAX 20
int n;

void read();
void sort();
void display();

struct list
{
    char name[20];
}names[MAX];

void main()
{
    clrscr();
    printf("\nSorting a Strings in Case Sensitive Order\n");
    read();
    sort();
    display();
    getch();
}

void read()
{
    int i;
    printf("Enter the no of names you want to enter(less than %d): ",MAX);
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
    printf("Enter name %d: ",i+1);
    scanf("%s",names[i].name);
    }
}

void sort()
{
    int i,j;
    struct list temp;
    for(i=1;i<n;i++)
    {
for(j=1;j<=n-i;j++)
{
   if(comparison(names[j-1].name,names[j].name)>0)
   {
   temp=names[j];
   names[j]=names[j-1];
   names[j-1]=temp;
   }
}
    }
}

void display()
{
    int i;
    for(i=0;i<n;i++)
    {
printf("Name %d: %s\n",i+1,names[i].name);
    }
}

int comparison(char *s,char *t)
{
    while((*s)==(*t))
    {
    s++;
    t++;
    }
    if((*s)>(*t))
    return 1;
    else
    return -1;

}


Output:



Saturday, January 8, 2011

C Program to Sorting a characters in the String

Coding:


#include<stdio.h> 
#include<string.h> 
#include<conio.h> 
void main()
{
char a[200],temp;
int n=0,j,i;
clrscr();
textcolor(23);
cprintf("\nEnter the string: ");
gets(a);
while(a[n]!='\0')
{
n++;
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
textcolor(23);
cprintf("\nThe string in alphabetical order:");
for(i=0;i<n;i++)
{
textcolor(10);
cprintf("%c",a[i]);
}
getch();
}

Output:



C Program for Prime Numbers upto n Number

Coding:


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,j,c;
clrscr();
textcolor(10);
 cprintf("\nEnter Number Of Terms::");
scanf("%d",&n);
textcolor(12);
cprintf("\nPrime Numbers Are Follwing:\r\n\n");
while(i<=n)
{
   c=0;
   for(j=1;j<=i;j++)
   {
     if(i%j==0)
c++;
   }
  if(c==2)
  printf("%28d\n",i);
  i++;
}
getch();
}

Output:



C Program to convert Numbers into Words

Coding:


#include<stdio.h>
#include<conio.h>

void pw(long,char[]);
char *one[]={" ",
    " One"," Two"," Three"," Four",
    " Five"," Six"," Seven"," Eight",
    " Nine"," Ten"," Eleven"," Twelve",
    " Thirteen"," Fourteen"," Fifteen",
    " Sixteen"," Seventeen"," Eighteen",
    " Nineteen"
    };

char *ten[]={ " "," "," Twenty"," Thirty"," Forty",
     " Fifty"," Sixty","Seventy"," Eighty",
     " Ninety"
    };


void main()
{
 long n;
 clrscr();
 printf("\n\n Enter any 9 digit no: ");
 scanf("%ld",&n);
  printf("\n");
 if(n<=0)
printf("Enter numbers greater than 0");
 else
 {

 pw((n/10000000),"Crore");
 pw(((n/100000)%100),"Lakh");
 pw(((n/1000)%100),"Thousand");
 pw(((n/100)%10),"Hundred");
 pw((n%100)," ");
 }
 getch();
}


void pw(long n,char ch[])
{
 textcolor(YELLOW);
 (n>19)?cprintf("%s %s ",ten[n/10],one[n%10]):cprintf("%s ",one[n]);
 if(n)
 cprintf("%s ",ch);
}

Output: