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:



C Program to find Factorial with and without using Recursion Function

Coding:


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

long fact(long);
void main()
{
int i;long f;

/* Initialization for Factorial without using Recursion
int i,n;
long f=1;   */

clrscr();
printf("\nEnter the number: ");
scanf("%d",&i);
f=fact(i);

/* Factorial without using Recursion
for(n=1;n<=i;n++)
f*=n;  */

printf("\nFactorial of %d is %ld",i,f);
getch();
}

long fact(long k)
{
if(k==0)
return 1;
else
return k*(fact(k-1));

}

Output:


C Program to find Largest number of 'n' numbers

Coding:

#include<stdio.h> 
#include<conio.h> 
void main()
{
int n,m,i,max;
clrscr();
printf("\nHow many numbers are you going to enter:");
scanf("%d",&n);
printf("\nEnter the numbers:\n");
textcolor(YELLOW);
scanf("%d",&m);
max=m;
for(i=2;i<=n;i++)
{
scanf("%d",&m);
if(m>max)
max=m;
}
cprintf("\nThe Largest Number is ");
textcolor(GREEN);
cprintf("%d",max);
getch();
}

Output:


Thursday, January 6, 2011

C Program to find a number of lines,words and characters from a given string

Coding:



#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
   {
int l=1,w=0,chars=1;
char c;
clrscr();
textcolor(9);
cprintf("\n********Terminate String by pressing the Tab Key followed by Enter Key ********");
printf("\n\nEnter the String :\n\n");
c=getchar();
while(c!= '\t')
{
c=getchar();
chars++;
if(c==' '|| c=='\n'||c=='.')
w++;
if(c=='\n')
l++;
if(c=='\t')
chars--;
}
textbackground(WHITE);
cprintf("\r\n--------------------------  ");
cprintf("\r\nNumber of Lines      =%d    ",l);
cprintf("\r\nNumber of Words      =%d    ",w+1);
cprintf("\r\nNumber of Characters =%d    ",chars);
cprintf("\r\n--------------------------  ");
getch();
}


Output:



Program to find number of Characters and Words in C Programming

Coding:

#include <stdio.h>
#include <string.h>
#include <conio.h>
void main()
   {

char str[200];
int i=0,word=0,ch=0;
clrscr();
textcolor(YELLOW);
printf("\nEnter the String (Max=200 characters): ")  ;
gets(str);
while(str[i]!='\0')
{
if(str[i]==' ')
{
word++;
ch++;
}
else
{
ch++;
}
i++;
}
printf("\nNumber of Characters = ");
cprintf("%d",ch);
printf("\nNumber of Words      = ");
cprintf("%d",word+1);
getch();

}
Output:

Program to find a Prime numbers in C Programming

Coding:

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j=0;
clrscr();
printf("\nEnter the number:");
scanf("%d",&n);

// *LOGIC*
for(i=1;i<=n;i++)
{
if(n%i==0)
j++;
}

if(j==2)
{
printf("\nEntered Number");
textcolor(YELLOW);
cprintf("%2d" ,n);
printf("is Prime");
}
else
{
printf("\nEntered Number");
textcolor(YELLOW);
cprintf("%2d",n);
printf(" is not a Prime",n);
}
getch();
}

Output: 








Wednesday, January 5, 2011

Reverse the string in C programming without using temp variable

Coding:

#include<stdio.h>
#include<conio.h>
void main()
{
char s[100];
int i,j;
clrscr();
printf("\nEnter the string : ");
gets(s);
i=0;
j=strlen(s)-1;
while(i < j) 


s[i]=s[i]+s[j];
s[j]=s[i]-s[j];
s[i]=s[i]-s[j--];
i++;

// using built-in function: 
//n=strrev(s);
printf("\nReverse string is : %s",s);
getch();
}


Output:






Reverse the string in C programming with temp variable

Coding:
 
#include<stdio.h>
#include<conio.h>
void main()
{
char s[100],temp;
int i,j;
clrscr();
printf("\n Enter the string :");
gets(s);
i=0;
j=strlen(s)-1;
while(i < j)

{
temp=s[i]; 
s[i]=s[j];
s[j--]=temp;
i++;
}
// using built-in function:
//n=strrev(s); 
printf("\nReverse string is : %s",s);
getch();
}

Output:

 

Program for reversing the string and Palindrome in C Programming

Coding: 
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50],a[50];
int i,len,j,n;
clrscr();
printf( "\n\nEnter the string to be reversed : " );
gets(str);


//copy one string to another without using the inbuilt function:
for(i=0;str[i]!='\0';i++)
a[i] = str[i];
a[i]='\0';


//using built-in function
//strcpy(a,str);


printf( "\nInput String : %s\n",str);
len=strlen(str)-1;
for(i=0;i < len;i++) 

{
str[i]=str[i]+str[len];
str[len]=str[i]-str[len];
str[i]=str[i]-str[len--];
}
j=strlen(str);
printf( "\nReversed String : %s\n",str); 
printf("\nLength of the string is %d\n",j);
//string compare using without built-in function: 
for(i=0;str[i]!='\0' &&a[i]!='\0';i++) 
if(str[i]!=a[i])
n=1;
//n=strcmp(str,a);------>>>Built-in function
// if you r using built function,change if condition as n==0
if(n!=1)
{
printf("\nThe Given String(or)Number is Palindrome");
}
else
{
printf("\nThe Given String(or)Number is not a Palindrome");
}
getch();
}



Output:


Tuesday, January 4, 2011

Inverted Triangles Pattern in C

coding:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,m;
char a='*';
clrscr();
printf("Enter the Number: ");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=1;j<=n-i;j++)

printf(" ");
for(j=1;j<=i;j++)

printf("%4c",a);
printf("\n");
}

for(i=2;i<=n;i++)

{
for(j=1;j<=n-i;j++)

printf(" ");
for(j=1;j<=i;j++)

printf("%4c",a);
printf("\n");
}
getch();
}


Output:

Printing Diamond Pattern in C Programming(r must be odd)

Coding:
#include<stdio.h>
#include <conio.h>
void main()
{
int r,s,c=1,j,k;
clrscr();
printf("Enter an Odd Number: ");
scanf("%d",&r);
printf("\n");
s=r/2;

for(j=0;j<=r/2;j++)
{
for(k=s;k>=0;k--)
printf(" ");
s--;
for(k=c;k>0;k--)
printf("*");
c+=2;
printf("\n");
}

c=r-2;
s+=2;

for(j=r/2;j>=0;j--)
{
for(k=0;k<=s;k++)
printf(" ");
s++;
for(k=c;k>0;k--)
printf("*");
c-=2;
printf("\n");
}

getch();

}

Output:

Printing Diamond Pattern in C


Coding:

#include<stdio.h>
#include <conio.h>
void main()
{
int i,j,n;
char a='*';
clrscr();
printf("Enter the Number: ");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(j=1;j<=i;j++)
printf("%-2c",a);
printf("\n");
}

n=n-1;

for(i=n;i>=1;i--)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(j=1;j<=i;j++)
printf("%2c",a);
printf("\n");
}
getch();
}

Output: