Showing posts with label Menu Driven Program. Show all posts
Showing posts with label Menu Driven Program. Show all posts

Menu driven program in C which performs the following operations on strings

How to write a menu driven program in C which performs the following operations on strings:
  1. Write separate function for each option 
  2. Check if one string is sub-string of another string 
  3. Count number of occurrences of a character in the string 
  4. Exit

Solution:

#include<stdio.h>
#include<conio.h>
#include<string.h>
  void substring();
  void char_occurrence();
  void main()
{
  char str[30],str1[30];
  int ch;
  clrscr();
  do
{
  printf("\n****MENU****");
  printf("\n1:Sub String");
  printf("\n2:Character Occurrences");
  printf("\n3:Exit");
  printf("\nEnter your choice: ");
  scanf("%d",&ch);

  switch(ch)
{
  case 1:substring();
  break;

  case 2:char_occurrance();
  break;

  case 3:exit(0);
  break;
}
}
  while(ch!=3);
  getch();
}

  void substring()
{
  char str[30],ch[30];
  fflush(stdin);
  printf("\nEnter the first string: ");
  gets(str);
  fflush(stdin);
  printf("\nEnter the second string: ");
  gets(ch);
  if(strstr(str,ch))
{
  printf("\nThe given string is sub string");
}
  else
{
  printf("\nThe given string is not sub string");
}
}

  void char_occurrennce()
{
  char str2[30],ch;
  int i,count=0;
  fflush(stdin);
  printf("\nEnter the string: ");
  gets(str2);
  fflush(stdin);
  printf("\nEnter the character from string to find occurrence: ");
  scanf("%c",&ch);
  for(i=0;str2[i]!='\0';i++)
{
  if(ch==str2[i])
{
  count++;
}
}
  printf("\nThe occurrence of character is: %d",count);
}

//OUTPUT:
//****MENU****
//1:Sub String
//2:Character Occurrences
//3:Exit

//Enter your choice: 1

//Enter the first string: vinit

//Enter the second string: vinit

//The given string is sub string

//****MENU****
//1:Sub String
//2:Character Occurrences
//3:Exit
//Enter your choice: 2

//Enter the string: vinit

//Enter the character from string to find occurrence: v

//The occurrence of character is: 1

//****MENU****
//1:Sub String
//2:Character Occurrences
//3:Exit
//Enter your choice: 3

Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list

How to write a C Program in Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list in C Programming Language ?



/*Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list */

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

struct node* addofll(struct node*); //prototype of the new linked list block creation
void display(struct node*); // prototype to display the elements in the linked list
void search(struct node*); //prototype for searching to check if the element is in the list
struct node* insert(struct node*); //prototype for inserting a node into the linked list
struct node* delete(struct node*); //prototype for deletion of a node from the linked list

/*creating the struct for the block of linked list*/

struct node  //struct node is required for linked list so as to get data and address
{
int data;
struct node* ptr; // struct node type pointer is required so as to hold the pointer of other struct node type for linking
};

int main()
{

struct node* head; //this pointer holds on the starting value of the data
int i,b;
head=NULL;

while(1)
{
printf("\nwhat do you want to do now?\nPress \n1-> to proceed for element processing \n2-> to display the elements \n3-> to search any element in the list \n4->Insert a node into linked list\n5->Delete the node from linked list\n");
scanf("%d",&b);

switch(b)
{
case 1: head=addofll(head);
break;

case 2: display(head);
break;

case 3: search(head);
break;

case 4: head=insert(head);
break;

case 5: head=delete(head);
break;

default: printf("You have entered an invalid option");
}
}
return 0;
}

void search(struct node*head)
{

int ele;
printf("Enter the element to be searched \n");
scanf("%d",&ele);

if(head!=NULL)
{
while (head!=NULL)
{
if(head->data==ele)
{
printf("\nthe element %d is found in the data\n",ele);
head=head->ptr;
}

else
{
while(head != NULL && head->data != ele)
{
head=head->ptr;

}
}

}

printf("\nThe search didnt find any element later\n");
}
else
printf("\nThere are no elements in the list\nPlease insert a new node\n");

}

void display(struct node* head)
{
printf("the data is displayed below\n");
while(head !=NULL)
{
printf("\n%d \t",head->data);
head=head->ptr;
}
}


struct node* addofll(struct node* head)
{
int dat;
printf("Enter data \n");
scanf("%d",&dat);
struct node* newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=dat;
newnode->ptr=NULL;

newnode->ptr=head;
return newnode;

}



struct node* insert(struct node* head)
{
int info,num;
struct node* newnode;
struct node* temp;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the value to be inserted\n");
scanf("%d",&info);

newnode->data=info;

printf("\nPoint out the location of insertion in terms of data of linked list\n");
scanf("%d",&num);

temp=head;

if(temp == NULL)
{
printf("\nThe list is empty\nThe new node is the first node\n");
newnode->ptr=NULL;
return newnode;
}


while(temp !=NULL && temp->data !=num)
temp=temp->ptr;


if(temp !=NULL && temp->data ==num)
{
printf("\nyou have found the location of the insertion: inserting %d in list\n",info);
newnode->ptr=temp->ptr;
temp->ptr=newnode;
}

printf("\nyou are out of the loop\n");

return head;


}


struct node* delete(struct node* head)
{
struct node *temp,*del;
int num;
printf("\nEnter the lisked list data element to be removed\n");
scanf("%d",&num);
temp=head;

if(temp !=NULL && temp->data == num)
{
printf("\nThe value is detected and this node is getting deleted\n");
head=temp->ptr;
free(temp);
return head;
}
if(temp !=NULL && temp->ptr->data ==num)
{
del=temp->ptr;
temp->ptr=temp->ptr->ptr;
free(del);
return head;
}
while(temp !=NULL && temp-> data != num)
{
if(temp !=NULL && temp->data !=num) {
temp=temp->ptr;
}
else
return head;
}
if(temp == NULL)
{
printf("\nThere are no elements in the list\nCreate a new node first\n");
return 0;
}


}

Menu Driven Program to Read Two Integers Find Sum, Difference and Product C Program

How to write C Program a menu driven program to read two integers and find their sum, difference & product in C Programming Language ?



Solution:
/* Write a menu driven program to read two integers & find their sum, difference & product. */

#include<stdio.h>

int main()
{

signed int a[2];
signed int sum,diff,prod;
printf("Enter the two numbers to find sum,difference,product \n");
scanf("%d%d",&a[0],&a[1]);
int option='s';

printf("Enter the character \n 1 -> to find sum \n 2 -> to find difference \n 3 -> to find the product \n ");
scanf("%d",&option);

switch(option)

case 1: {
sum=a[0]+a[1];
printf("the sum of the two numbers is %d \n",sum);
  //break;
}

case 2: {
diff=a[0]-a[1];
printf("the sum of the two numbers is %d \n",diff);
//break;
}

case 3: {
prod=a[0]*a[1];
printf("the sum of the two numbers is %d \n",prod);
//break;
}

default: printf("you have entered the wrong character");

return 0;
}