Beruflich Dokumente
Kultur Dokumente
Department of BES:FED
AY 2016-2017(Even Semester)
1. (a)
Examples:
Input : ABcED
Output :3
Sol)
#include<stdio.h>
#include<conio.h>
#define size 26
Example :
Ans)
#include<stdio.h>
#include<conio.h>
// structure of node of doubly linked list
struct Node
{
int data;
struct Node *next, *prev;
};
// Function to find pair whose sum equal to given value x.
void pairSum(struct Node *head, int x)
{
// Set two pointers, first to the beginning of DLL
// and second to the end of DLL.
struct Node *first = head;
struct Node *second = head;
int found=0;
while (second->next != NULL)
second = second->next;
while (first != NULL && second != NULL &&
first != second && second->next != first)
{
// pair found
if ((first->data + second->data) == x)
{
found = 1;
printf("(%d,%d)",first->data,second->data);
// move first in forward direction
first = first->next;
// move second in backward direction
second = second->prev;
}
else
{
if ((first->data + second->data) < x)
first = first->next;
else
second = second->prev;
}
}
// if pair is not present
if (found ==0)
printf("No pair found");
}
// A utility function to insert a new node at the
// beginning of doubly linked list
void insert(struct Node **head, int data)
{
struct Node *temp = (struct Node *)malloc(sizeof(struct
Node));//new Node;
temp->data = data;
temp->next = temp->prev = NULL;
if (!(*head))
(*head) = temp;
else
{
temp->next = *head;
(*head)->prev = temp;
(*head) = temp;
}
}
int main()
{
struct Node *head = NULL;
int x;
clrscr();
insert(&head, 11);
insert(&head, 10);
insert(&head, 9);
insert(&head, 8);
insert(&head, 7);
insert(&head, 6);
insert(&head, 3);
x = 17;
pairSum(head, x);
return 0;
}
output: The largest word is programmers and The smallest word is here in amazing
programmers exists here.
solution:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char text[]="Amazing programmers exists here ";
char t1[10];
char str[10][20];
int i,j,k,c=0;
int l[20];
int min=0,r;
clrscr();
puts(text);
for(i=0;text[i]!='\0';i++)
{ if(text[i]==' '||text[i]=='\n')
c++;
}
k=0;
j=0;
for(i=0;text[i]!='\0';i++)
{
if(text[i]==' ')
{
t1[j]='\0';
strcpy(str[k++],t1);
j=0;
}
else
{
t1[j++]=text[i];
}
}
for(i=0;i<k;i++)
{
l[i]=strlen(str[i]);
}
min=l[0];
for(i=0;i<k;i++)
if(l[i]<min)
{
min=l[i];
r=i;
}
printf("\nsmallest word in given string is: %s",str[r]);
getch();
}
3(a) Given a line of text T and a pattern(substring) P. Write an algorithm that deletes 1st
occurrence of P in T.
Algorithm
1.Start
2.Read the main string and the substring
3. For each character in main string until length of it or until \0 is reached
4. compare it with each character in substring
5. If a mismatch occurs then set found=0;
6. Finally if found=1, then move the characters from right to left
i.etext[j] = text[j + len1];
7. Stop
//Instead of steps 3 &4 one can use strstr() function in string.h to get the pointer to first
occurrence of the substring in the main string.
////SYNTAX:char *strstr(char * mainstring,char *substring)
Program
#include <stdio.h>
#include <string.h>
#define SIZE 100
len = strlen(text);
len1 = strlen(substring);
if(found == 1)
{
for(j=i; j<len-len1; j++)
{
text[j] = text[j + len1];
}
text[j] = '\0';
break;
}
}
}
3.(b) Write a program to create and print out the elements of a Single Linked List.
struct node
{
int data;
struct node *link;
};
Typedef struct node * list;
//Use struct node * in place of list, if typedef is not used
//The below function takes start(or header) as argument and returns the same after creation is
completed.
4.(a) Write and Test a Function hydroxide that returns a 1 for true if its string argument
ends in the substring "OH". Try the function hydroxide on the following data:
KOH,H2O2,Nacl,NaOH,MgOH.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int hydroxide();
void main()
{
int res=0;
clrscr();
res=hydroxide();
if(res==1)
printf("\nFound");
else
printf("\n Not Found");
getch();
}
int hydroxide()
{
char str[10];
printf("\n Enter a String");
gets(str);
if(str[strlen(str)-2]=='O'&&str[strlen(str)-1]=='H')
return 1;
else
return 0;
}
4.(b) Write a function to sort the given DLL.
void sort()
{
int i, j, x;
temp2 = h;
temp4 = h;
if (temp2 == NULL)
{
printf("\n List empty to sort");
return;
}
5(a) Define a structure data type named date containing three integer members day,
month, year. Write a function to read the data into structure members and print the
date in the following format using pointers: January 09, 2001. {5 Marks}
#include<stdio.h>
struct date
{
int dd,mm,yyyy;
} *d;
void main()
{
printf(Enter the date with day, month and year as dd, mm, yyyy format );
scanf(%d%d%d, &d->dd, &d->mm, &d->yyyy);
switch(d->mm)
{
case 1: printf(January); break;
case 2: printf(February); break;
case 3: printf(March); break;
case 4 printf(April); break;
case 5: printf(May);break;
case 6: printf(June);break;
case 7: printf(July);break;
case 8: printf(August); break;
case 9: printf(September); break;
case 10: printf(October); break;
case 11: printf(November); break;
case 12: printf(December); break;
}
printf( %d %d, d->dd, d->yyyy);
}
5(b) Given an integer k, write an algorithm that deletes the kth element in doubly linked
list. Program: {5 marks}
struct node
{
int data;
struct node *next, *prev;
void delete_element_at_any_Position()
{
int i,k,ele;
printf(Enter the position to be deleted);
scanf(%d,&k);
ele = p->data;
free(p);
printf(The deleted data from the list is = %d ,ele);
}
Algorithm:
1) Define the structure node for three fields 1)data 2) next 3) prev
2) Get the position of the node to be deleted.
3) Stop the pointer p at the position of the node to be deleted and pointer t at
previous to the p pointer.
4) Make the t pointer to be pointing to next to the p pointer.
5) Make the p -> next of prev to point to t pointer.
6) Display the deleted value on the screen.
7) Finally delete the node totally from memory.
8) Stop the operation.
#include<stdio.h>
#include<string.h>
void main()
{
char str[20];
int i,l,j;
printf(enter a string);
scanf( %s,str);
l=strlen(str);
for(i=0;str[i]!=\0i++)
{
if(isalpha(str[i]))==0)
{
for(j=i;j<l;j++)
{
str[j]=str[j+1];
}
}
}
printf(output string is %s,str);
}
b) Write a function find() that returns a pointer to the node containing a specific key.
The function should take single linked list as input parameter.
#include <stdio.h>
struct node
{
int data;
struct node *next;
};
struct node *start=NULL;
struct node * find(struct node *temp, int key)
{
while(temp!=NULL)
{
if(temp->data==key)
{
return(temp);
}
}
return(NULL);
}
void main()
{
int key;
printf(enter key);
scanf(%d,&key);
if(find(start)==NULL)
printf(key is not in the list);
else
printf(key is in the list);
}