Session-1:
Q) Given an array containing sequence of bits(0 or 1),you have to sort this array in the ascending
order in all 0’s in first part of array followed by all 1’s.The constraints is that you can swap only the
adjacent elements in the array. Find the minimum number of swaps required to sort the given input
array.
Ex: Given array (0,0,1,0,1,0,1,1)the minimum number of swaps is 3.
Program:
#include<stdio.h>
main()
{
int a[100],n,i,j,k,temp,count=0;
printf("enter the number of elements");
scanf("%d",&n);
printf("nenter the elements into array");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;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;
count++;
}
}
}
printf("n Number of swaps =%d",count);
}
Q) You are given an array of positive and negative integers. if a number n at an index is positive, then
move forward n steps. Conversely, if its negative, move backwards n steps. Determine if there is a loop
in this array. For Example given the array [2,-1,1,2,2] index 0 maps to index 2,1 maps to 0,2 maps to 3
and so on. There is a loop in this array because 0 maps to 2,2 maps to 3,3 maps to 0.(Use the modulo
operator)
Program:
#include<stdio.h>
#include<stdlib.h>
main()
{
int a[10],n,i,k,x,j,c,t;
printf("enter the size of array");
scanf("%d",&n);
printf("enter the elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
x=k=0;
c=0;
while(c<n) //Atmost we can have maximum of n jumps
{
if(a[k]==0)
{
printf("element value cannot be zero");
exit(0);
}
else if(a[k]>0)
{
k=(k+a[k])%n;
}
else if(a[k]<0)
{
k=(k+a[k])%n;
if(k<0)
k=n+k;
}
if(x==k)
{
printf("loop exists");
exit(0);
}
c++;
}
}
Session-2
Q) Explain nested structures with the following:
Consider student structure with the following members name, register number, DOB. DOB is a nested
structure storing the day month and year. Find the youngest student in the class and sort the details
according to their DOB. Implement using pointers and DMA.
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
struct dob
{
int day,mon, year;
};
struct student
{
unsigned long int num;
char name[30];
struct dob d;
}*s[100],*max;
int n,i,j;
printf("n Enter number of students");
scanf("%d",&n);
for(i=0;i<n;i++)
{
s[i]=(struct student *)(malloc(sizeof(struct student)));
printf("n Enter Number");
scanf("%uld",&s[i]->num);
printf("n Enter Name");
scanf("%s",s[i]->name);
printf("n Enter Date of Birth(dd-mm-yy");
scanf("%d%d%d",&s[i]->d.day,&s[i]->d.mon,&s[i]->d.year);
}
printf("n Student aren");
for(i=0;i<n;i++)
{
printf("n Number :%u",s[i]->num);
printf("n Name:%s",s[i]->name);
printf("n DOB:%d-%d-%d",s[i]->d.day,s[i]->d.mon,s[i]->d.year);
}
max=(struct student *)malloc(sizeof(struct student));
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(s[i]->d.year>s[j]->d.year||s[i]->d.year==s[j]->d.year&&s[i]->d.mon>s[j]->d.mon||s[i]-
>d.year==s[j]->d.year&&s[i]->d.mon==s[j]->d.mon&&s[i]->d.day>s[j]->d.day)
{
max=s[i];
s[i]=s[j];
s[j]=max;
}
}
}
for(i=0;i<n;i++)
{
printf("n %u",s[i]->num);
printf("n %s",s[i]->name);
printf("n %d-%d-%d",s[i]->d.day,s[i]->d.mon,s[i]->d.year);
}
getch();
}
Q) Have the following structs, for Book, Author and Shelf. Book has multiple authors and Shelf has
multiple books. Write a program to create a functions new_author (), new_book (), print_book (),
add_author_to_book (), new_shelf (), delete_shelf (), print_shelf(), add_book_to_shelf() with required
return types and parameters.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Author
{
char name[50];
};
struct Book
{
char title[50];
struct Author authors[5];
int ac;
};
struct Shelf
{
struct Book books[30];
int bc;
};
struct Shelf * new_shelf()
{
struct Shelf *shelf = malloc(sizeof(struct Shelf));
shelf->bc=0;
return shelf;
}
void delete_shelf(struct Shelf *shelf)
{
free(shelf);
}
struct Author new_author(char name[])
{
struct Author author;
strcpy(author.name,name);
return author;
}
struct Book new_book(char title[])
{
struct Book book;
strcpy(book.title,title);
book.ac=0;
return book;
}
void print_book(struct Book book)
{
int i,n;
printf("%sn",book.title);
printf("Authors aren");
for(i=0,n=book.ac;i<n;i++)
{
printf("%sn",book.authors[i].name);
}
printf("n");
}
void print_shelf(struct Shelf *shelf)
{
int i,n;
printf("Shelf has the %d booksn",shelf->bc);
for(i=0,n=shelf->bc;i<n;i++)
{
print_book(shelf->books[i]);
}
}
void add_author_to_book(struct Book *book, struct Author author)
{
book->authors[book->ac]=author;
book->ac++;
}
void add_book_to_shelf(struct Shelf *shelf, struct Book book)
{
shelf->books[shelf->bc]=book;
shelf->bc++;
}
int main()
{
int ch;
struct Shelf *shelf = new_shelf();
struct Book book = new_book("CandDataStructures");
add_author_to_book(&book,new_author("MARKALLENWEISS"));
add_author_to_book(&book,new_author("FOUROUZAN"));
add_book_to_shelf(shelf,book);
print_shelf(shelf);
delete_shelf(shelf);
return 0;
}
Session-3
Q)Consider a structure name with its members as
firstname(20chars),middlename(1char).lastname(20chars),include the structure name in a employee
structure with empcode as member ,structure name as its members.the empcodes to be stored are
E01,E02 ..so on.Write a C program to input names of 5 employees and print out their initials of
each(e.g., Swamy K Puri should be printed as SKP)along with their code using pointers.(TROUBLE
FREE C by HARI MOHAN PANDEY).
Program:
#include <stdio.h>
struct name
{
char firstname[20];
char middlename;
char lastname[20];
};
struct employee
{
char empcode[4];
struct name ename;
};
main()
{
struct employee e[2];
int i;
for(i=0;i<2;i++)
{
printf("enter employee code");
scanf("%s",&e[i].empcode);
printf("enter firstname and lastname of employee");
scanf("%s %c%s",&e[i].ename.firstname,&e[i].ename.middlename,&e[i].ename.lastname);
printf("%c%c%c",e[i].ename.firstname[0],e[i].ename.middlename,e[i].ename.lastname[0]);
}
}
Q)
Consider a Structure Cricket with its members as Team1,Team2,Groundplayed,Result.Write a program to
display the information in a tabular form in a Sorted order based on no of matches won by a team
Ex:
Team No of matches
won
INDIA 3
PAK 2
AUS 1
Program:
#include <stdio.h>
#include <string.h>
#define NM 6
#define NT 3
struct Cricket
{
char team1[20];
char team2[20];
char ground[18];
int result;
};
struct Cricket match[NM] ={{"IND","AUS","Pune",1},
{"PAK","IND","Nagpur",0},
{"AUS","PAK","HYD",1},
{"AUS","IND","CHE",0},
{"AUS","PAK","MUM",1},
{"PAK","AUS","BZA",1}};
struct result
{
int won;
char team[5];
}r[NT];
bubblesort()
{
struct result temp;
int i,j;
for(i=0;i<NT-1;i++)
{
for(j=0;j<NT-i-1;j++)
{
if(r[j].won <r[j+1].won)
{
temp=r[j];
r[j]=r[j+1];
r[j+1]=temp;
}
}
}
}
main()
{
int i,j;
// for(i=0;i<NM;i++)
// {
// printf("enter team1, team 2, ground played, result");
// scanf("%s %s %s %d",match[i].team1,match[i].team2,match[i].ground,&match[i].result);
// }
printf("enter the team name");
for(j=0;j<NT;j++)
{
scanf("%s",r[j].team);
r[j].won=0;
for(i=0;i<NM;i++)
{
if(strcmpi(r[j].team,match[i].team1)== 0 && match[i].result == 1)
r[j].won++;
if(strcmpi(r[j].team,match[i].team2)== 0 && match[i].result == 0)
r[j].won++;
}
}
bubblesort();
for(i=0;i<NT;i++)
printf("n%st%d",r[i].team,r[i].won);
}
Session-4
Q)A person went for an interview and one of the interviewer wants to call the person name in shortcut
by taking all the starting characters in his name write a c code which prints initials of any name.
Sample output:
Enter a string: Vera Venkata Satya Lakshman
VVSL(www.cprograms.com)
Program:
#include<stdio.h>
int main(){
char str[20];
int i=0;
printf("Enter a string: ");
gets(str);
printf("%c",*str);
while(str[i]!='0'){
if(str[i]==' '){
printf("%c",*(str+i+1));
}
i++;
}
return 0;
}
Q) Developa c code for the following function( using Pointer-function).
char Letter(char* [],n)
{
…………….
……………
…………….
}
Sample Input
4
abc
abcba
abcd
cba
Sample Output
2
0
4
2
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void letter(char *s[], int n)
{
int i,l1,j,k,diff,c=0;
for(i=0;i<n;i++)
{
scanf("%s",s[i]);
l1=strlen(s[i])-1;
for(j=0;j<l1;j++,l1--)
{
diff=*(*(s+i)+j)-*(*(s+i)+l1);
c=c+abs(diff);
*(*(s+i)+l1)=*(*(s+i)+l1)+diff;
}
printf("%dn",c);
puts(s[i]);
}
}
main()
{
int T,i;
char *s[10],s1[10];
printf("enter the number of test cases");
scanf("%d",&T);
for(i=0;i<T;i++)
s[i]=(char *)malloc(sizeof(s1));
letter(s,T);
}
Q)Write a c program to INSERT A SUBSTRING AT A PARTICULAR POSITION
Program:
#include <stdio.h>
#include <string.h>
main()
{
char s1[10],s2[10],s3[10];
int i,j,l1,l2,pos;
gets(s1);
gets(s2);
l1=strlen(s1);
l2=strlen(s2);
printf("enter the position");
scanf("%d",&pos);
strcpy(s3,s1);
j=pos-1;
for(i=0;i<l2;i++,pos++)
{
s3[pos]=s2[i];
}
for(i=j;i<l1+l2;i++,pos++)
{
s3[pos]=s1[i];
}
s3[pos]='0';
printf("%s",s3);
}
Session-5
Q) wacp to check if String is anagram or not(USING POINTERS.)(HackerEarth): Note: A word x is
an anagram of another word y if we can produce y by rearranging the letters of x.
#include <stdio.h>
#include <string.h>
int main (void) {
char s1[] = "abcd";
char s2[] = "bcad";
char temp;
int i,d, j;
int n = strlen(s1);
int n1 = strlen(s2);
if( n != n1) {
printf("%s and %s are not anagrams! n", s1, s2);
return 0;
}
for (i = 0; i < n; i++)
{
for (j =0; j < n; j++)
{
if (s1[j] > s1[j+1])
{
temp = s1[j];
s1[j] = s1[j+1];
s1[j+1] = temp;
}
if (s2[j] > s2[j+1])
{
temp = s2[j];
s2[j] = s2[j+1];
s2[j+1] = temp;
}
}
}
d=strcmp(s1,s2);
if(d==0)
printf("Strings are anagrams");
else printf("Strings are not anagrams! n");
return 0;
}
Q)Develop a code for the following function
Sample Input
2
JACK
DANIEL
ABACABA
ABACABA
Sample Output
DAJACKNIEL
AABABACABACABA
Program:
#include<stdio.h>
#include<string.h>
int main()
{
char s1[20],s2[20],s3[20];
int l1,l2,i,j,k;
printf("enter the 1st string");
gets(s1);
l1=strlen(s1);
printf("enter 2nd string");
gets(s2);
l2=strlen(s2);
i=j=k=0;
while(i<l1&&j<l2)
{
if(s1[i]<s2[j])
s3[k++]=s1[i++];
else
s3[k++]=s2[j++];
}
while(i<l1)
{
s3[k++]=s1[i++];
}
while(j<l2)
{
s3[k++]=s2[j++];
}
s3[l1+l2]='0';
puts(s3);
}
Q) Wacp to find all substrings of a given string using (function pointers).
Eg: EG:Enter a String :
"Code”
Substring of “Code” are c,co,cod,code,o,od,ode,d,de,e
Program:
#include <stdio.h>
#include <string.h>
main()
{
char name[50];
int i,j,k,l1;
gets(name);
l1=strlen(name);
for(i=0;i<l1;i++)
{
for(j=0;j<l1;j++)
{
for(k=i;k<=j;k++)
printf("%c",name[k]);
printf("n");
}
}
}
Session-6
Q) Suppose the Personnel file of a small company contain the following data for all its employees
SSN(social security number),Name and salary. A Linked list is used to store the Ask students to write
a function to sort the records based on SSN?
Program:
#include <stdio.h>
#include <string.h>
struct node
{
int ssn,sal;
char name[50];
struct node *next;
};
struct node *head=NULL,*c,*p;
void create()
{
int value,s,opt;
char n[50];
while(1)
{
printf("Press 1 to continue and Press 0 to stop");
scanf("%d",&opt);
if(opt == 1)
{
printf("enter SSN, name and sal");
scanf("%d%s%d",&value,n, &s);
struct node * new = (struct node *)malloc(sizeof(struct node));
new->next=NULL;
new->ssn=value;
strcpy(new->name,n);
new->sal=s;
if(head == NULL)
head = new;
else
{
c=head;
while(c->next != NULL)
{
c=c->next;
}
c->next=new;
}
}
else
return;
}
}
display()
{
if(head == NULL)
printf("list is empty");
else
{
c=head;
while(c->next!=NULL)
{
printf("n%dt%st%dn",c->ssn,c->name,c->sal);
c=c->next;
}
printf("n%dt%st%dn",c->ssn,c->name,c->sal);
}
}
main()
{
int n,i,temp,j,t1,t2;
char ntemp[50];
create();
// display();
if(head != NULL)
{
c=head;
for(c=head;c->next != NULL;c=c->next)
{
for(p=c->next;p!=NULL;p=p->next)
{
if(c->ssn > p->ssn)
{
t1=c->ssn;t2=c->sal;strcpy(ntemp,c->name);
c->ssn=p->ssn;c->sal=p->sal;strcpy(c->name,p->name);
p->ssn=t1;p->sal=t2;strcpy(p->name,ntemp);
}
}
}
}
display();
}
Q) Write a program to reverse the given string using Single linked list.
Program:
#include <stdio.h>
#include <string.h>
struct node
{
char data;
struct node *next;
};
struct node *head=NULL,*c,*p,*r;
void create(char value)
{
struct node * new = (struct node *)malloc(sizeof(struct node));
new->data = value;
new->next=NULL;
if(head == NULL)
head = new;
else
{
c=head;
while(c->next != NULL)
{
c=c->next;
}
c->next=new;
}
}
display()
{
if(head == NULL)
printf("list is empty");
else
{
c=head;
while(c->next!=NULL)
{
printf("n %ct",c->data);
c=c->next;
}
printf(" %cn",c->data);
}
}
main()
{
char s1[50];
int i;
printf("enter a string");
scanf("%s",s1);
for(i=0;s1[i]!='0';i++)
create(s1[i]);
c=head;p=NULL;
while(c!=NULL)
{
r=p;
p=c;
c=c->next;
p->next=r;
}
head=p;
display();
}
Session-7

C programs

  • 1.
    Session-1: Q) Given anarray containing sequence of bits(0 or 1),you have to sort this array in the ascending order in all 0’s in first part of array followed by all 1’s.The constraints is that you can swap only the adjacent elements in the array. Find the minimum number of swaps required to sort the given input array. Ex: Given array (0,0,1,0,1,0,1,1)the minimum number of swaps is 3. Program: #include<stdio.h> main() { int a[100],n,i,j,k,temp,count=0; printf("enter the number of elements"); scanf("%d",&n); printf("nenter the elements into array"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n-1;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; count++; } } } printf("n Number of swaps =%d",count); } Q) You are given an array of positive and negative integers. if a number n at an index is positive, then move forward n steps. Conversely, if its negative, move backwards n steps. Determine if there is a loop in this array. For Example given the array [2,-1,1,2,2] index 0 maps to index 2,1 maps to 0,2 maps to 3 and so on. There is a loop in this array because 0 maps to 2,2 maps to 3,3 maps to 0.(Use the modulo operator) Program: #include<stdio.h> #include<stdlib.h> main() { int a[10],n,i,k,x,j,c,t; printf("enter the size of array"); scanf("%d",&n); printf("enter the elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); x=k=0;
  • 2.
    c=0; while(c<n) //Atmost wecan have maximum of n jumps { if(a[k]==0) { printf("element value cannot be zero"); exit(0); } else if(a[k]>0) { k=(k+a[k])%n; } else if(a[k]<0) { k=(k+a[k])%n; if(k<0) k=n+k; } if(x==k) { printf("loop exists"); exit(0); } c++; } } Session-2 Q) Explain nested structures with the following: Consider student structure with the following members name, register number, DOB. DOB is a nested structure storing the day month and year. Find the youngest student in the class and sort the details according to their DOB. Implement using pointers and DMA. Program: #include<stdio.h> #include<conio.h> #include<stdlib.h> main() { struct dob { int day,mon, year; }; struct student { unsigned long int num; char name[30]; struct dob d; }*s[100],*max; int n,i,j;
  • 3.
    printf("n Enter numberof students"); scanf("%d",&n); for(i=0;i<n;i++) { s[i]=(struct student *)(malloc(sizeof(struct student))); printf("n Enter Number"); scanf("%uld",&s[i]->num); printf("n Enter Name"); scanf("%s",s[i]->name); printf("n Enter Date of Birth(dd-mm-yy"); scanf("%d%d%d",&s[i]->d.day,&s[i]->d.mon,&s[i]->d.year); } printf("n Student aren"); for(i=0;i<n;i++) { printf("n Number :%u",s[i]->num); printf("n Name:%s",s[i]->name); printf("n DOB:%d-%d-%d",s[i]->d.day,s[i]->d.mon,s[i]->d.year); } max=(struct student *)malloc(sizeof(struct student)); for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(s[i]->d.year>s[j]->d.year||s[i]->d.year==s[j]->d.year&&s[i]->d.mon>s[j]->d.mon||s[i]- >d.year==s[j]->d.year&&s[i]->d.mon==s[j]->d.mon&&s[i]->d.day>s[j]->d.day) { max=s[i]; s[i]=s[j]; s[j]=max; } } } for(i=0;i<n;i++) { printf("n %u",s[i]->num); printf("n %s",s[i]->name); printf("n %d-%d-%d",s[i]->d.day,s[i]->d.mon,s[i]->d.year); } getch(); } Q) Have the following structs, for Book, Author and Shelf. Book has multiple authors and Shelf has multiple books. Write a program to create a functions new_author (), new_book (), print_book (), add_author_to_book (), new_shelf (), delete_shelf (), print_shelf(), add_book_to_shelf() with required return types and parameters. Program: #include <stdio.h> #include <stdlib.h> struct Author {
  • 4.
    char name[50]; }; struct Book { chartitle[50]; struct Author authors[5]; int ac; }; struct Shelf { struct Book books[30]; int bc; }; struct Shelf * new_shelf() { struct Shelf *shelf = malloc(sizeof(struct Shelf)); shelf->bc=0; return shelf; } void delete_shelf(struct Shelf *shelf) { free(shelf); } struct Author new_author(char name[]) { struct Author author; strcpy(author.name,name); return author; } struct Book new_book(char title[]) { struct Book book; strcpy(book.title,title); book.ac=0; return book; } void print_book(struct Book book) { int i,n; printf("%sn",book.title); printf("Authors aren"); for(i=0,n=book.ac;i<n;i++) { printf("%sn",book.authors[i].name); } printf("n"); } void print_shelf(struct Shelf *shelf) { int i,n; printf("Shelf has the %d booksn",shelf->bc); for(i=0,n=shelf->bc;i<n;i++)
  • 5.
    { print_book(shelf->books[i]); } } void add_author_to_book(struct Book*book, struct Author author) { book->authors[book->ac]=author; book->ac++; } void add_book_to_shelf(struct Shelf *shelf, struct Book book) { shelf->books[shelf->bc]=book; shelf->bc++; } int main() { int ch; struct Shelf *shelf = new_shelf(); struct Book book = new_book("CandDataStructures"); add_author_to_book(&book,new_author("MARKALLENWEISS")); add_author_to_book(&book,new_author("FOUROUZAN")); add_book_to_shelf(shelf,book); print_shelf(shelf); delete_shelf(shelf); return 0; } Session-3 Q)Consider a structure name with its members as firstname(20chars),middlename(1char).lastname(20chars),include the structure name in a employee structure with empcode as member ,structure name as its members.the empcodes to be stored are E01,E02 ..so on.Write a C program to input names of 5 employees and print out their initials of each(e.g., Swamy K Puri should be printed as SKP)along with their code using pointers.(TROUBLE FREE C by HARI MOHAN PANDEY). Program: #include <stdio.h> struct name { char firstname[20]; char middlename; char lastname[20]; }; struct employee { char empcode[4]; struct name ename; }; main() { struct employee e[2]; int i; for(i=0;i<2;i++)
  • 6.
    { printf("enter employee code"); scanf("%s",&e[i].empcode); printf("enterfirstname and lastname of employee"); scanf("%s %c%s",&e[i].ename.firstname,&e[i].ename.middlename,&e[i].ename.lastname); printf("%c%c%c",e[i].ename.firstname[0],e[i].ename.middlename,e[i].ename.lastname[0]); } } Q) Consider a Structure Cricket with its members as Team1,Team2,Groundplayed,Result.Write a program to display the information in a tabular form in a Sorted order based on no of matches won by a team Ex: Team No of matches won INDIA 3 PAK 2 AUS 1 Program: #include <stdio.h> #include <string.h> #define NM 6 #define NT 3 struct Cricket { char team1[20]; char team2[20]; char ground[18]; int result; }; struct Cricket match[NM] ={{"IND","AUS","Pune",1}, {"PAK","IND","Nagpur",0}, {"AUS","PAK","HYD",1}, {"AUS","IND","CHE",0}, {"AUS","PAK","MUM",1}, {"PAK","AUS","BZA",1}}; struct result { int won; char team[5]; }r[NT]; bubblesort() { struct result temp; int i,j; for(i=0;i<NT-1;i++) {
  • 7.
    for(j=0;j<NT-i-1;j++) { if(r[j].won <r[j+1].won) { temp=r[j]; r[j]=r[j+1]; r[j+1]=temp; } } } } main() { int i,j; //for(i=0;i<NM;i++) // { // printf("enter team1, team 2, ground played, result"); // scanf("%s %s %s %d",match[i].team1,match[i].team2,match[i].ground,&match[i].result); // } printf("enter the team name"); for(j=0;j<NT;j++) { scanf("%s",r[j].team); r[j].won=0; for(i=0;i<NM;i++) { if(strcmpi(r[j].team,match[i].team1)== 0 && match[i].result == 1) r[j].won++; if(strcmpi(r[j].team,match[i].team2)== 0 && match[i].result == 0) r[j].won++; } } bubblesort(); for(i=0;i<NT;i++) printf("n%st%d",r[i].team,r[i].won); } Session-4 Q)A person went for an interview and one of the interviewer wants to call the person name in shortcut by taking all the starting characters in his name write a c code which prints initials of any name. Sample output: Enter a string: Vera Venkata Satya Lakshman VVSL(www.cprograms.com) Program: #include<stdio.h> int main(){ char str[20]; int i=0; printf("Enter a string: "); gets(str); printf("%c",*str); while(str[i]!='0'){
  • 8.
    if(str[i]==' '){ printf("%c",*(str+i+1)); } i++; } return 0; } Q)Developa c code for the following function( using Pointer-function). char Letter(char* [],n) { ……………. …………… ……………. } Sample Input 4 abc abcba abcd cba Sample Output 2 0 4 2 Program: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> void letter(char *s[], int n) { int i,l1,j,k,diff,c=0; for(i=0;i<n;i++) { scanf("%s",s[i]); l1=strlen(s[i])-1; for(j=0;j<l1;j++,l1--) { diff=*(*(s+i)+j)-*(*(s+i)+l1); c=c+abs(diff); *(*(s+i)+l1)=*(*(s+i)+l1)+diff; } printf("%dn",c); puts(s[i]); } } main() { int T,i; char *s[10],s1[10]; printf("enter the number of test cases"); scanf("%d",&T); for(i=0;i<T;i++) s[i]=(char *)malloc(sizeof(s1));
  • 9.
    letter(s,T); } Q)Write a cprogram to INSERT A SUBSTRING AT A PARTICULAR POSITION Program: #include <stdio.h> #include <string.h> main() { char s1[10],s2[10],s3[10]; int i,j,l1,l2,pos; gets(s1); gets(s2); l1=strlen(s1); l2=strlen(s2); printf("enter the position"); scanf("%d",&pos); strcpy(s3,s1); j=pos-1; for(i=0;i<l2;i++,pos++) { s3[pos]=s2[i]; } for(i=j;i<l1+l2;i++,pos++) { s3[pos]=s1[i]; } s3[pos]='0'; printf("%s",s3); } Session-5 Q) wacp to check if String is anagram or not(USING POINTERS.)(HackerEarth): Note: A word x is an anagram of another word y if we can produce y by rearranging the letters of x. #include <stdio.h> #include <string.h> int main (void) { char s1[] = "abcd"; char s2[] = "bcad"; char temp; int i,d, j; int n = strlen(s1); int n1 = strlen(s2); if( n != n1) { printf("%s and %s are not anagrams! n", s1, s2); return 0; } for (i = 0; i < n; i++) { for (j =0; j < n; j++) { if (s1[j] > s1[j+1]) {
  • 10.
    temp = s1[j]; s1[j]= s1[j+1]; s1[j+1] = temp; } if (s2[j] > s2[j+1]) { temp = s2[j]; s2[j] = s2[j+1]; s2[j+1] = temp; } } } d=strcmp(s1,s2); if(d==0) printf("Strings are anagrams"); else printf("Strings are not anagrams! n"); return 0; } Q)Develop a code for the following function Sample Input 2 JACK DANIEL ABACABA ABACABA Sample Output DAJACKNIEL AABABACABACABA Program: #include<stdio.h> #include<string.h> int main() { char s1[20],s2[20],s3[20]; int l1,l2,i,j,k; printf("enter the 1st string"); gets(s1); l1=strlen(s1); printf("enter 2nd string"); gets(s2); l2=strlen(s2); i=j=k=0; while(i<l1&&j<l2) { if(s1[i]<s2[j]) s3[k++]=s1[i++];
  • 11.
    else s3[k++]=s2[j++]; } while(i<l1) { s3[k++]=s1[i++]; } while(j<l2) { s3[k++]=s2[j++]; } s3[l1+l2]='0'; puts(s3); } Q) Wacp tofind all substrings of a given string using (function pointers). Eg: EG:Enter a String : "Code” Substring of “Code” are c,co,cod,code,o,od,ode,d,de,e Program: #include <stdio.h> #include <string.h> main() { char name[50]; int i,j,k,l1; gets(name); l1=strlen(name); for(i=0;i<l1;i++) { for(j=0;j<l1;j++) { for(k=i;k<=j;k++) printf("%c",name[k]); printf("n"); } } } Session-6 Q) Suppose the Personnel file of a small company contain the following data for all its employees SSN(social security number),Name and salary. A Linked list is used to store the Ask students to write a function to sort the records based on SSN? Program: #include <stdio.h> #include <string.h> struct node { int ssn,sal; char name[50]; struct node *next; };
  • 12.
    struct node *head=NULL,*c,*p; voidcreate() { int value,s,opt; char n[50]; while(1) { printf("Press 1 to continue and Press 0 to stop"); scanf("%d",&opt); if(opt == 1) { printf("enter SSN, name and sal"); scanf("%d%s%d",&value,n, &s); struct node * new = (struct node *)malloc(sizeof(struct node)); new->next=NULL; new->ssn=value; strcpy(new->name,n); new->sal=s; if(head == NULL) head = new; else { c=head; while(c->next != NULL) { c=c->next; } c->next=new; } } else return; } } display() { if(head == NULL) printf("list is empty"); else { c=head; while(c->next!=NULL) { printf("n%dt%st%dn",c->ssn,c->name,c->sal); c=c->next; } printf("n%dt%st%dn",c->ssn,c->name,c->sal); } } main() { int n,i,temp,j,t1,t2; char ntemp[50];
  • 13.
    create(); // display(); if(head !=NULL) { c=head; for(c=head;c->next != NULL;c=c->next) { for(p=c->next;p!=NULL;p=p->next) { if(c->ssn > p->ssn) { t1=c->ssn;t2=c->sal;strcpy(ntemp,c->name); c->ssn=p->ssn;c->sal=p->sal;strcpy(c->name,p->name); p->ssn=t1;p->sal=t2;strcpy(p->name,ntemp); } } } } display(); } Q) Write a program to reverse the given string using Single linked list. Program: #include <stdio.h> #include <string.h> struct node { char data; struct node *next; }; struct node *head=NULL,*c,*p,*r; void create(char value) { struct node * new = (struct node *)malloc(sizeof(struct node)); new->data = value; new->next=NULL; if(head == NULL) head = new; else { c=head; while(c->next != NULL) { c=c->next; } c->next=new; } } display() { if(head == NULL) printf("list is empty"); else
  • 14.
    { c=head; while(c->next!=NULL) { printf("n %ct",c->data); c=c->next; } printf(" %cn",c->data); } } main() { chars1[50]; int i; printf("enter a string"); scanf("%s",s1); for(i=0;s1[i]!='0';i++) create(s1[i]); c=head;p=NULL; while(c!=NULL) { r=p; p=c; c=c->next; p->next=r; } head=p; display(); } Session-7