Pages

Custom Search
Showing posts with label c programs. Show all posts
Showing posts with label c programs. Show all posts

Saturday, August 9, 2014

Program to reverse the order of words in a string

Program to reverse the order of words in a string.
For Eg:
Input: this is a simple c program
Output: program c simple a is this

The LOGIC part:
-First determine the length of the string. Use a simple for loop
until you find '\0' else use an inbuilt string function 'strlen()' like I have used.
-then move over to the last word in your string.
Place a pointer at the end of the last word and then find the
space before that word and place a pointer after the space
and using these two pointer append the word to a new string.
-Repeat procedure for all words from last word to first word.


SOURCE CODE:

 #include <stdio.h>  
 #include <stdlib.h>  
 int main()  
 {  
      char str[100], rev[100];  
      int i, j, k, m, n, len, c=0, flag=1;  
      printf("\nEnter a sentence: ");  
      gets(str);  
      printf("\nEntered sentence: ");  
      puts(str);  
      len = strlen(str)-1;          //length of string except \0  
      i=0;  
      j=len;  
      k=0;  
      while(flag==1)                    //while more words to process  
      {  
           flag = 1;  
           i=j;                         //pointer i, j to end of current word  
           while(str[i] != ' ')     //decrement pointer i till beginning of current word  
           {  
                if(i==0)  
                {  
                     flag = 0;          //special case for 1st word of the sentence  
                     break;  
                }  
                i--;  
           }  
            m=i;  
           if(flag)  
           {  
                i=i+1;       
           }       
           for( ; i<=j; )               //append current word to new char array  
           {  
                rev[k++] = str[i++];  
           }  
           j=m-1;                         //move pointer j to end of previous word  
           rev[k++]=' ';  
      }  
      rev[k]='\0';  
      printf("\nReverse sentence: ");  
      puts(rev);  
      printf("\n");  
      return 1;  
 }  


SAMPLE OUTPUT:

Monday, October 10, 2011

Program to check if entered string is palindrome or not using stack.


/*A string is said to be palindrome when the reverse of the string is same as that of the original string
   For ex: Entered string: madam... is a palindrome*/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define SIZE 10

typedef struct
{
        int items[SIZE];
        int top;

}STACK;


void push();
int pop();
void display();
int isoverflow();
int isempty();

int main()
{
        STACK s;
        char str[100];
        int i, flag;

        s.top = -1;

        printf("\nEnter a string: ");
        gets(str);

        for(i=0;str[i]!='\0'; i++)
                  push(&s, str[i]);

        flag = 1;
        for(i=0;str[i]!='\0';i++)
        {
                if(str[i] != pop(&s))
                {
                        flag = 0;
                        break;
                }
        }

if(flag == 1)
                printf("\nEntered string is palindrome\n");
        else
                printf("\nEntered string is not a palindrome\n");

}

void push(STACK *p, int element)
{
           if(isoverflow(p->top))
           {
                    printf("\nStack is overflow");
           }
           else
          {
                  (p->top)++;
                   p->items[p->top] = element;
          }
}

int pop(STACK *p)
{
         if(isempty(p->top))
         {
                  printf("\nStack is underflow");
         }
         else
         {
                  return (p->items[(p->top)--]);
         }
}

void display(STACK s)
{
        int i;
        if(isempty(s.top))
        {
printf("\nStack is empty");
        }
        else
        {
                for(i=s.top; i>=0; i--)
                {
                        printf("\n%d", s.items[i]);
                }
        }
}

//Function to check stack overflow condition
int isoverflow(int top)
{
        if(top == SIZE - 1)
                return (1);
        else
                return (0);
}

//Function to check stack empty condition
int isempty(int top)
{
         if(top == -1)
                return (1);
        else
                return (0);
}

SAMPLE OUTPUT:




Program to print entered numbers in words


//For ex: if enetered number is '1234' the output will be "One Two Three Four"

#include<stdio.h>
#include<stdlib.h>

void main()
{
        int digit, num, rev=0, n, m, i, count=0;
        printf("\nEnter an integer: ");
        scanf("%d", &num);
        m=num;
        while(num>0)
        {
                digit=num%10;
                count++;
                num=num/10;
                rev=rev*10+digit;
        }
        n=rev;
        rev=0;
        if(m%10!=0)
        {


                while(n>0)
                {
                        digit=n%10;
                        switch(digit)
                        {
                                case 1 : printf(" One");
                                            break;
                                case 2 : printf(" Two");
                                            break;
                                case 3 : printf(" Three");
                                            break;
                                case 4 : printf(" Four");
                                            break;
                                case 5 : printf(" Five");
                                            break;
                                case 6 : printf(" Six");
                                             break;
                                case 7 : printf(" Seven");
                                             break;
                                case 8 : printf(" Eight");
                                             break;
                                case 9 : printf(" Nine");
                                             break;
                                case 0 : printf(" Zero");
                                             break;
 
                        }
                        n=n/10;
                        rev=rev*10+digit;
                }
        }
        else
        {
                for(i=0;i<count;i++)
                {
                        digit=n%10;
                        switch(digit)
                        {
                                case 1 : printf(" One");
                                             break;
                                case 2 : printf(" Two");
                                             break;
                                case 3 : printf(" Three");
                                            break;
                                case 4 : printf(" Four");
                                             break;
                                case 5 : printf(" Five");
                                             break;
                                case 6 : printf(" Six");
                                             break;
                                case 7 : printf(" Seven");
                                             break;
                                case 8 : printf(" Eight");
                                             break;
                                case 9 : printf(" Nine");
                                             break;
                                case 0 : printf(" Zero");
                                             break;
                        }
                         n=n/10;
                }
        }
         printf("\n");
        printf("\nPress ENTER to EXIT\n");
        getch();
}

SAMPLE OUTPUT:

Wednesday, October 5, 2011

Two stacks in a single linear array.

Stack is a data structure in which last inserted element is the last deleted element. The term 'push' is used to insert an element, 'pop is used to delete an element. Stack is also known as LIFO(Last In First Out).
In this program we are implementing two stacks in a single linear array and the two stacks grow towards each other i.e., first stack starts from the front of the array and the second stack starts from the end of the array. And the total number of elements in both the stacks can vary.

/*Include the necessary header files*/

#define MAX 10

typedef struct
{
int stack[MAX]; //Array of integers
int top1;
int top2;
}STACK;



void push1();
void pop1();
void display1();
void push2();
void pop2();
void display2(); //Function prototypes


int main()
{
STACK s;
int choice;
s.top1 = -1;
s.top2 = MAX;
do
{
printf("\n\nOperation on two stacks:");
printf("\n\nStack 1\t\t\tStack 2");
printf("\n__________\t\t__________");
printf("\n\n1.) Push1\t\t4.) Push2");
printf("\n2.) Pop1\t\t5.) Pop2");
printf("\n3.) Display1\t\t6.) Display2");
printf("\n7.) Exit");
printf("\n\nEnter your choice: ");
scanf("%d", &choice);
switch( choice )
{
case 1: push1(&s);
break;
case 2: pop1(&s);
break;
case 3: display1(s);
break;
case 4: push2(&s);
break;
case 5: pop2(&s);
break;
case 6: display2(s);
break;
case 7: break;
default: printf("\nInvalid choice! Enter again.");
}
}while(choice!=7);
getch();
}

void push1(STACK *p)
{
if(p->top1 == (p->top2) - 1)
{
printf("\nStack1 is overflow");
}
else
{
(p->top1)++;
printf("\nEnter the element to push: ");
scanf("%d", &p->stack[p->top1]);
}
}

void pop1(STACK *p)
{
if(p->top1 == -1)
{
printf("\nStack1 is underflow");
}
else
{
printf("\nPopped element: %d", p->stack[(p->top1)--]);
}
}

void display1(STACK s)
{
int i;
if(s.top1 == -1)
{
printf("\nStack1 is Empty");
}
else
{
printf("\nStack1 is: \n");
for(i=s.top1; i>=0; i--)
{
printf("\n%d", s.stack[i]);
}
}
}

void push2(STACK *p)
{
if(p->top2 == p->top1 + 1)
{
printf("\nStack2 is overflow");
}
else
{
(p->top2)--;
printf("\nEnter the element to push: ");
scanf("%d", &p->stack[p->top2]);
}
}

void pop2(STACK *p)
{
if(p->top2 == MAX)
{
printf("\nStack2 is underflow");
}
else
{
printf("\nPopped element: %d", p->stack[(p->top2)++]);
}
}

void display2(STACK s)
{
int i;
if(s.top2 == MAX)
{
printf("\nStack2 is Empty");
}
else
{
printf("\nStack2 is: \n");
for(i=s.top2 ; i < MAX ; i++)
{
printf("\n%d", s.stack[i]);
}
}
}


ORDINARY QUEUE:

Queue is data structure in which the first inserted element is the first deleted element. It is also known as a FIFO(First In First Out). The program implementation using structure is as follows.


/*Include the necessary header files(stdio.h, stdlib.h, conio.h)*/

#define QSIZE 5 //Maximum elements in Queue

struct tag //structure definition
{
int items[QSIZE];
int rear;
int front;
};
typedef struct tag QUEUE;

void insert();
int Delete();
void display(); //Function Prototypes

int main()
{
QUEUE q;
int element, dnum, choice;
q.rear = -1;
q.front = 0;

do
{
printf("\n\nOperations on Ordinary Queue: ");
printf("\n\n1.) INSERT");
printf("\n\n2.) DELETE");
printf("\n\n3.) DISPLAY");
printf("\n\n4.) QUIT");
printf("\n\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("\nEnter the element into queue: ");
scanf("%d", &element);
insert(&q, element); //Function call by reference
break;
case 2: dnum = Delete(&q); // Function call by reference
if(dnum != -1)
{
printf("\nDeleted element: %d", dnum);
}
break;

case 3: display(q); //Function call by value
break;
case 4: break;
default: printf("\nInvalid choice! Enter again.");
}
}while(choice!=4);
getch();
}

//Function to insert elements into the queue

void insert(QUEUE *q, int element)
{
if(q->rear == QSIZE - 1)
{
printf("\nQueue is full.");
return;
}
else
{
(q->rear)++;
q->items[q->rear] = element;
}
}

//Function to delete an element from the front of queue.

int Delete(QUEUE *q)
{
if(q->front > q->rear)
{
printf("\nQueue is empty.");
return(-1);
}
else
{
return(q->items[(q->front)++]);
}
}

//Function to display the contents of queue from front to rear.

void display(QUEUE q)
{
int i;
if(q.front > q.rear)
printf("\n\nQueue empty.");
else
{
printf("\nQueue is: \n\n");
for(i=q.front; i<=q.rear; i++ )
{
printf("%d ", q.items[i]);
}
}
}

Custom Search