-STACKS
(im using dashes to maintain the indentation here)
--creating the structure
Struct stack
{
Char data [MAX];
int top;
};
--declaring the functions
char pop (struct stack *stk);
void push (struct stack *stk, char item);
--initializing in Main Program(Function)
main()
{
stack s;
s.top = -1;
push( &s , "A" ) // Calling the Push Function while inserting letter "A" into a stack
pop( &s ) // Removing the element
}
--PUSH Function
void push (stack *stk, char item)
{
If (stk ->top < MAX -1)
{
stk->top++;
stk->data[stk->top] = item;
}
Else
printf(“stack is full \n”);
}
--POP Function
char pop (stack *stk)
{
If(stk->top > -1 )
{
char x = srk -> data[stk->top];
stk->top--;
return x;
}
Else
{
printf(“stack is empty \n”);
return 0;
}
}
Google It, You'll Get It.
Monday, March 28, 2011
Stack - Real Life Example - Plates in Dispenser
EBook to learn stacks _ Free
Stacks - Introduction
-Stack is the First Data Structure in Programming.
-It is a method where we use LIFO ( LAST IN - FIRST OUT ).
LIFO stacks, also known as "push down" stacks, are the conceptually simplest way of saving information in a temporary storage location for such common computer operations as mathematical expression evaluation and recursive subroutine calling.
-It has only two fundamental operations called PUSH and POP.
--PUSH > to insert an element ( to the top )
The push operation adds an item to the top of the stack,
hiding any items already on the stack, or initializing the stack if it is empty.
--POP > to remove an element ( from the top )
The pop operation removes an item from the top of the stack, and returns this value to the caller. A pop either reveals previously concealed items, or results in an empty stack.
-It is a method where we use LIFO ( LAST IN - FIRST OUT ).
LIFO stacks, also known as "push down" stacks, are the conceptually simplest way of saving information in a temporary storage location for such common computer operations as mathematical expression evaluation and recursive subroutine calling.
-It has only two fundamental operations called PUSH and POP.
--PUSH > to insert an element ( to the top )
The push operation adds an item to the top of the stack,
hiding any items already on the stack, or initializing the stack if it is empty.
--POP > to remove an element ( from the top )
The pop operation removes an item from the top of the stack, and returns this value to the caller. A pop either reveals previously concealed items, or results in an empty stack.
Saturday, February 5, 2011
String starting with 'b; in a given series of strings
header file stdio.h
header file string.h
main()
{
int i;
const int stringLimit = 5;
char s[stringLimit][50]; //row and column
printf("enter the strings \n");
for(i=0;i
/*for(i=0;i
printf("str %s \n",s[i]);*/
printf("\n strings Starting wIth letter 'b' \n");
for(i=0;i
printf("word %d : %s \n",i+1,s[i]);
}
Pig - Latin phrase from English language phrase ( Converting words into pig latin )
input : computer
output: omputercay ( omputer + c + ay )
-----------------------------------------------
input : jump
output: umpjay ( ump + j + ay )
-----------------------------------------------
//header file stdio.h
//header file string.h
main()
{
int i=0,wordcount=0;
char word[50];
char reorder[50];
char frst;
printf("enter the word : ");
gets(word);
frst = word[0];
for( i=0 ; word[i+1] != 0 ; i++)
{
reorder[i]=word[i+1];
}
reorder[i]=frst;
reorder[i+1]='a';
reorder[i+2]='y';
reorder[i+3]='\0';
printf("the pigLatin word is %s \n",reorder);
}
output: omputercay ( omputer + c + ay )
-----------------------------------------------
input : jump
output: umpjay ( ump + j + ay )
-----------------------------------------------
//header file stdio.h
//header file string.h
main()
{
int i=0,wordcount=0;
char word[50];
char reorder[50];
char frst;
printf("enter the word : ");
gets(word);
frst = word[0];
for( i=0 ; word[i+1] != 0 ; i++)
{
reorder[i]=word[i+1];
}
reorder[i]=frst;
reorder[i+1]='a';
reorder[i+2]='y';
reorder[i+3]='\0';
printf("the pigLatin word is %s \n",reorder);
}
Monday, January 31, 2011
Scan and delete a desired a word from a sentence
#include
main()
{
int i=0;
int length = 0;
int word = 1;
int remove=0;
int tempw=1;
//read the string
char strng[50];
printf("enter the string : ");
gets(strng);
printf("Saved String : ");
puts(strng);
//printf("Initial values : Length %d, word %d, wotd numb %d, i %d. \n",length, word, wn, i);
//calculate the length
while(strng[i]!='\0')
{
length++;
i++;
}
printf("String Length : %d \n",length);
// calculate the no of words
i=0;
word=1;
while(i
if(strng[i]==' ')
word++;
i++;
}
printf("nO. of Words : %d \n",word);
//askng for the word to delete
printf("Enter the word number you need to delete : ",word);
scanf("%d",&remove);
//scaning for that word
for(i=0;i
tempw++; // 2nd word count
if(tempw==remove)
printf("%c",strng[i]);
}
}
Scan a Character win a string and replace it if its available
#include<'stdio.h'>
main()
{
char name[20];
//input
printf("enter the name : ");
scanf("%s",&name);
//out
printf("**%s** \n",name);
//count
int count=1;
for(count=0;name[count]!='\0';count++);
printf("count %d \n",count);
flushall();
/*
char n;
printf("enter the letter : ");
scanf("%c",&n);
int ncount = 0, i =0;
for(i=0;i<=count;i++)
if(name[i]==n)
ncount++;
printf("ncount %d \n",ncount);*/
//scanng lettr
char n;
printf("enter the letter (scan) : ");
scanf("%c",&n);
flushall();
//replacing letter
char m;
printf("enter the letter (replace): ");
scanf("%c",&m);
int i =0;
for(i=0;i<=count;i++)
if(name[i]==n)
name[i]=m;
printf("%s",name);
}
main()
{
char name[20];
//input
printf("enter the name : ");
scanf("%s",&name);
//out
printf("**%s** \n",name);
//count
int count=1;
for(count=0;name[count]!='\0';count++);
printf("count %d \n",count);
flushall();
/*
char n;
printf("enter the letter : ");
scanf("%c",&n);
int ncount = 0, i =0;
for(i=0;i<=count;i++)
if(name[i]==n)
ncount++;
printf("ncount %d \n",ncount);*/
//scanng lettr
char n;
printf("enter the letter (scan) : ");
scanf("%c",&n);
flushall();
//replacing letter
char m;
printf("enter the letter (replace): ");
scanf("%c",&m);
int i =0;
for(i=0;i<=count;i++)
if(name[i]==n)
name[i]=m;
printf("%s",name);
}
Palindrome Word checker
What are palindrome Words? http://rinkworks.com/words/palindromes.shtml
#include
main()
{
char word[10], test='T';
int l = 0, i = 0; // l=length
printf("enetr the word : ");
scanf("%s",&word);
//length checkng
for (i=0;word[i]!='\0';i++)
{
l++; // length of the word
}
printf("wrd count %d \n",l);
//check
/* while(i!=l)
{
while( word[i]==word[l-i] )
{
printf("match");
i++;
//j--;
}
}
*/
//checkz
l=i-1;
for(i=0;i<(l/2);i++)
{
if(word[i]!=word[l-i])
test='F';
}
if(test=='T')
printf("Yes");
else
printf("No");
}
#include
main()
{
char word[10], test='T';
int l = 0, i = 0; // l=length
printf("enetr the word : ");
scanf("%s",&word);
//length checkng
for (i=0;word[i]!='\0';i++)
{
l++; // length of the word
}
printf("wrd count %d \n",l);
//check
/* while(i!=l)
{
while( word[i]==word[l-i] )
{
printf("match");
i++;
//j--;
}
}
*/
//checkz
l=i-1;
for(i=0;i<(l/2);i++)
{
if(word[i]!=word[l-i])
test='F';
}
if(test=='T')
printf("Yes");
else
printf("No");
}
String Counter in Simple C
#include<'stdio.h'>
main()
{
char fname[10];
char lname[10];
int fc=0,lc=0,i=0;
printf("----in----\n");
scanf("%s",&fname);
scanf("%s",&lname);
printf("----out----\n");
printf("%s \n",fname);
printf("%s \n",lname);
i=0;
while(fname[i] != '\0')
{
fc++;
i++;
}
i=0;
while(lname[i] != '\0')
{
lc++;
i++;
}
printf("\nfrst : %d \n",fc);
printf("lst : %d \n",lc);
}
main()
{
char fname[10];
char lname[10];
int fc=0,lc=0,i=0;
printf("----in----\n");
scanf("%s",&fname);
scanf("%s",&lname);
printf("----out----\n");
printf("%s \n",fname);
printf("%s \n",lname);
i=0;
while(fname[i] != '\0')
{
fc++;
i++;
}
i=0;
while(lname[i] != '\0')
{
lc++;
i++;
}
printf("\nfrst : %d \n",fc);
printf("lst : %d \n",lc);
}
Using %s for strings + char array
#include<'stdio.h'>
main()
{
char fname[10];
char lname[10];
printf("----in----\n");
scanf("%s",&fname);
scanf("%s",&lname);
printf("----out----\n");
printf("%s \n",fname);
printf("%s \n",lname);
}
main()
{
char fname[10];
char lname[10];
printf("----in----\n");
scanf("%s",&fname);
scanf("%s",&lname);
printf("----out----\n");
printf("%s \n",fname);
printf("%s \n",lname);
}
Larges and the smallest number in a series
#include
main()
{
int in[5], temp=0 ,i=0,j;
//----input
printf("Enter 5 digits \n\n");
for(i=0;i<5;i++)
{
scanf("%d",&in[i]);
}
//-----sort
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(in[i]>in[j])
{
temp=in[i];
in[i] = in[j];
in[j] = temp;
}
}
}
//-----output
/*printf("\nafter sorting \n\n");
for(i=0;i<5;i++)
{
printf("%d \n",in[i]);
} */
//Print the samllest
printf("the smallest number : %d \n",in[0]);
//Print the largest
printf("the largest number : %d \n",in[4]);
}
main()
{
int in[5], temp=0 ,i=0,j;
//----input
printf("Enter 5 digits \n\n");
for(i=0;i<5;i++)
{
scanf("%d",&in[i]);
}
//-----sort
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(in[i]>in[j])
{
temp=in[i];
in[i] = in[j];
in[j] = temp;
}
}
}
//-----output
/*printf("\nafter sorting \n\n");
for(i=0;i<5;i++)
{
printf("%d \n",in[i]);
} */
//Print the samllest
printf("the smallest number : %d \n",in[0]);
//Print the largest
printf("the largest number : %d \n",in[4]);
}
Sorting
#include<'stdio.h'>
main()
{
int in[5], temp=0 ,i=0,j;
//----input
printf("Enter 5 digits \n\n");
for(i=0;i<5;i++)
{
scanf("%d",&in[i]);
}
//-----sort
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(in[i]>in[j])
{
temp=in[i];
in[i] = in[j];
in[j] = temp;
}
}
}
//-----output
printf("\nafter sorting \n\n");
for(i=0;i<5;i++)
{
printf("%d \n",in[i]);
}
}
main()
{
int in[5], temp=0 ,i=0,j;
//----input
printf("Enter 5 digits \n\n");
for(i=0;i<5;i++)
{
scanf("%d",&in[i]);
}
//-----sort
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(in[i]>in[j])
{
temp=in[i];
in[i] = in[j];
in[j] = temp;
}
}
}
//-----output
printf("\nafter sorting \n\n");
for(i=0;i<5;i++)
{
printf("%d \n",in[i]);
}
}
using nested for loops to built a numeric triangle
o:
1
22
333
4444
----------------------
#include<'stdio.h'>
main()
{
for(int loop=1; loop<=4;loop++)
{
for(int j=0; j {
printf("%d",loop);
}
printf("\n");
}
}
1
22
333
4444
----------------------
#include<'stdio.h'>
main()
{
for(int loop=1; loop<=4;loop++)
{
for(int j=0; j
printf("%d",loop);
}
printf("\n");
}
}
Using If and Else with decimal points in printf
#include<'stdio.h'>
main()
{
float sal,tot, inc;
printf("enter your salary : ");
scanf("%f",&sal);
if( sal < 10000 )
{inc=sal*1.345/100;
tot=(sal*101.345)/100; }
else if( sal < 25000 )
{inc=sal*0.75/100;
tot=(sal*100.75)/100; }
else if( sal >=25000 )
{inc=sal*0.456/100;
tot=(sal*100.456)/100; }
printf("\n\n");
printf("Basic Sal : %.2f \n",sal);
printf("Increment : %.3f \n",inc);
printf("Total : %.2f \n",tot);
}
main()
{
float sal,tot, inc;
printf("enter your salary : ");
scanf("%f",&sal);
if( sal < 10000 )
{inc=sal*1.345/100;
tot=(sal*101.345)/100; }
else if( sal < 25000 )
{inc=sal*0.75/100;
tot=(sal*100.75)/100; }
else if( sal >=25000 )
{inc=sal*0.456/100;
tot=(sal*100.456)/100; }
printf("\n\n");
printf("Basic Sal : %.2f \n",sal);
printf("Increment : %.3f \n",inc);
printf("Total : %.2f \n",tot);
}
Counts Odds and Even Digits in a 5 Digit Number
#include<'stdio.h'>
main()
{
long numb, rem, i, temp = 0, sum, in, count = 0, even=0, odd=0;
printf("Input a 5 digit number \n");
scanf("%ld",&numb);
i = 1;
//printf("Input a digit \n");
//scanf("%ld",&in);
printf("\n");
while(i<=5)
{
temp=temp*10;
rem=numb%10;
//checking
if(rem%2>0)
odd++;
else
even++;
//if(rem==in)
//count++; //c= rem, numb =a
temp=temp+rem;
sum=sum+rem;
i++;
rem=0;
numb=numb/10;
}
//printf("%ld appears in here for %ld time \n",in,count);
printf("Total No. of Odd numberz : %ld \n",odd);
printf("Total No. of Even numberz : %ld \n",even);
// printf("inverse is %ld",temp);
}
main()
{
long numb, rem, i, temp = 0, sum, in, count = 0, even=0, odd=0;
printf("Input a 5 digit number \n");
scanf("%ld",&numb);
i = 1;
//printf("Input a digit \n");
//scanf("%ld",&in);
printf("\n");
while(i<=5)
{
temp=temp*10;
rem=numb%10;
//checking
if(rem%2>0)
odd++;
else
even++;
//if(rem==in)
//count++; //c= rem, numb =a
temp=temp+rem;
sum=sum+rem;
i++;
rem=0;
numb=numb/10;
}
//printf("%ld appears in here for %ld time \n",in,count);
printf("Total No. of Odd numberz : %ld \n",odd);
printf("Total No. of Even numberz : %ld \n",even);
// printf("inverse is %ld",temp);
}
Calculator using if and while to run till you need
#include<'stdio.h'>
#include<'conio.h'>
main()
{
int a, b, sum;
char choice = 'y';
char select;
while(choice == 'y')
{
printf("Enter the two values : \n");
scanf("%d %d",&a,&b );
flushall();
printf("/n/n MENU ");
printf(" a. Addition ");
printf(" b. Subtraction ");
printf(" c. Multiplication ");
printf(" d. Division ");
printf(" e. Exit /n/n");
printf("/n Select from above : ");
scanf("%c",&select);
flushall();
while(select != 'e')
{
if(select=='a')
sum = a + b;
if(select=='b')
sum = a - b;
if(select=='c')
sum = a * b;
if(select=='d')
sum = a / b;
printf("/n %d",sum);
sum = 0;
}
}
printf("Continue : ( Y or N )");
scanf("%c",&choice);
}
#include<'conio.h'>
main()
{
int a, b, sum;
char choice = 'y';
char select;
while(choice == 'y')
{
printf("Enter the two values : \n");
scanf("%d %d",&a,&b );
flushall();
printf("/n/n MENU ");
printf(" a. Addition ");
printf(" b. Subtraction ");
printf(" c. Multiplication ");
printf(" d. Division ");
printf(" e. Exit /n/n");
printf("/n Select from above : ");
scanf("%c",&select);
flushall();
while(select != 'e')
{
if(select=='a')
sum = a + b;
if(select=='b')
sum = a - b;
if(select=='c')
sum = a * b;
if(select=='d')
sum = a / b;
printf("/n %d",sum);
sum = 0;
}
}
printf("Continue : ( Y or N )");
scanf("%c",&choice);
}
Subscribe to:
Posts (Atom)

