SlideShare a Scribd company logo
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

More Related Content

What's hot

Ada file
Ada fileAda file
Ada file
Kumar Gaurav
 
Daa practicals
Daa practicalsDaa practicals
Daa practicals
Rekha Yadav
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
argusacademy
 
Os lab 1st mid
Os lab 1st midOs lab 1st mid
Os lab 1st mid
Murali Kummitha
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
Rahul Chugh
 
Computer graphics programs in c++
Computer graphics programs in c++Computer graphics programs in c++
Computer graphics programs in c++
Ankit Kumar
 
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Make Mannan
 
Programs for Operating System
Programs for Operating SystemPrograms for Operating System
Programs for Operating SystemLPU
 
Pnno
PnnoPnno
Cpds lab
Cpds labCpds lab
C Prog. - Strings (Updated)
C Prog. - Strings (Updated)C Prog. - Strings (Updated)
C Prog. - Strings (Updated)vinay arora
 
Cg my own programs
Cg my own programsCg my own programs
Cg my own programsAmit Kapoor
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
Harjinder Singh
 
Programs
ProgramsPrograms
Wap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithmWap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithm
Kapil Pandit
 
computer graphics practicals
computer graphics practicalscomputer graphics practicals
computer graphics practicals
Manoj Chauhan
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
AAlha PaiKra
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
Kandarp Tiwari
 

What's hot (20)

C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
 
Ada file
Ada fileAda file
Ada file
 
Daa practicals
Daa practicalsDaa practicals
Daa practicals
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
Os lab 1st mid
Os lab 1st midOs lab 1st mid
Os lab 1st mid
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Computer graphics programs in c++
Computer graphics programs in c++Computer graphics programs in c++
Computer graphics programs in c++
 
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
 
Programs for Operating System
Programs for Operating SystemPrograms for Operating System
Programs for Operating System
 
Pnno
PnnoPnno
Pnno
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Struct examples
Struct examplesStruct examples
Struct examples
 
C Prog. - Strings (Updated)
C Prog. - Strings (Updated)C Prog. - Strings (Updated)
C Prog. - Strings (Updated)
 
Cg my own programs
Cg my own programsCg my own programs
Cg my own programs
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
Programs
ProgramsPrograms
Programs
 
Wap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithmWap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithm
 
computer graphics practicals
computer graphics practicalscomputer graphics practicals
computer graphics practicals
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
 

Similar to C programs

Questions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdfQuestions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdf
apexelectronices01
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Er Ritu Aggarwal
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
Rumman Ansari
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
Ramachendran Logarajah
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
archana singh
 
Structured data type
Structured data typeStructured data type
Structured data type
Omkar Majukar
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
vrgokila
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
sowmya koneru
 
Kotlin for Android Developers - 2
Kotlin for Android Developers - 2Kotlin for Android Developers - 2
Kotlin for Android Developers - 2
Mohamed Nabil, MSc.
 
C programs
C programsC programs
C programs
Koshy Geoji
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
Hazrat Bilal
 
Cpd lecture im 207
Cpd lecture im 207Cpd lecture im 207
Cpd lecture im 207
Syed Tanveer
 
C programs
C programsC programs
C programs
Azaj Khan
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
Nithin Kumar,VVCE, Mysuru
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
Prof. Dr. K. Adisesha
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 

Similar to C programs (20)

array.ppt
array.pptarray.ppt
array.ppt
 
Questions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdfQuestions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdf
 
Coding
CodingCoding
Coding
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Vcs16
Vcs16Vcs16
Vcs16
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
 
Structured data type
Structured data typeStructured data type
Structured data type
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
 
Kotlin for Android Developers - 2
Kotlin for Android Developers - 2Kotlin for Android Developers - 2
Kotlin for Android Developers - 2
 
C programs
C programsC programs
C programs
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
 
Cpd lecture im 207
Cpd lecture im 207Cpd lecture im 207
Cpd lecture im 207
 
C programs
C programsC programs
C programs
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 

More from Lakshmi Sarvani Videla

Data Science Using Python
Data Science Using PythonData Science Using Python
Data Science Using Python
Lakshmi Sarvani Videla
 
Programs on multithreading
Programs on multithreadingPrograms on multithreading
Programs on multithreading
Lakshmi Sarvani Videla
 
Menu Driven programs in Java
Menu Driven programs in JavaMenu Driven programs in Java
Menu Driven programs in Java
Lakshmi Sarvani Videla
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
Lakshmi Sarvani Videla
 
Simple questions on structures concept
Simple questions on structures conceptSimple questions on structures concept
Simple questions on structures concept
Lakshmi Sarvani Videla
 
Errors incompetitiveprogramming
Errors incompetitiveprogrammingErrors incompetitiveprogramming
Errors incompetitiveprogramming
Lakshmi Sarvani Videla
 
Relational Operators in C
Relational Operators in CRelational Operators in C
Relational Operators in C
Lakshmi Sarvani Videla
 
Recursive functions in C
Recursive functions in CRecursive functions in C
Recursive functions in C
Lakshmi Sarvani Videla
 
Function Pointer in C
Function Pointer in CFunction Pointer in C
Function Pointer in C
Lakshmi Sarvani Videla
 
Functions
FunctionsFunctions
Java sessionnotes
Java sessionnotesJava sessionnotes
Java sessionnotes
Lakshmi Sarvani Videla
 
Singlelinked list
Singlelinked listSinglelinked list
Singlelinked list
Lakshmi Sarvani Videla
 
Graphs
GraphsGraphs
B trees
B treesB trees
Functions in python3
Functions in python3Functions in python3
Functions in python3
Lakshmi Sarvani Videla
 
Dictionary
DictionaryDictionary
Sets
SetsSets
Lists
ListsLists
Data Mining: Data Preprocessing
Data Mining: Data PreprocessingData Mining: Data Preprocessing
Data Mining: Data Preprocessing
Lakshmi Sarvani Videla
 
Dbms
DbmsDbms

More from Lakshmi Sarvani Videla (20)

Data Science Using Python
Data Science Using PythonData Science Using Python
Data Science Using Python
 
Programs on multithreading
Programs on multithreadingPrograms on multithreading
Programs on multithreading
 
Menu Driven programs in Java
Menu Driven programs in JavaMenu Driven programs in Java
Menu Driven programs in Java
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
Simple questions on structures concept
Simple questions on structures conceptSimple questions on structures concept
Simple questions on structures concept
 
Errors incompetitiveprogramming
Errors incompetitiveprogrammingErrors incompetitiveprogramming
Errors incompetitiveprogramming
 
Relational Operators in C
Relational Operators in CRelational Operators in C
Relational Operators in C
 
Recursive functions in C
Recursive functions in CRecursive functions in C
Recursive functions in C
 
Function Pointer in C
Function Pointer in CFunction Pointer in C
Function Pointer in C
 
Functions
FunctionsFunctions
Functions
 
Java sessionnotes
Java sessionnotesJava sessionnotes
Java sessionnotes
 
Singlelinked list
Singlelinked listSinglelinked list
Singlelinked list
 
Graphs
GraphsGraphs
Graphs
 
B trees
B treesB trees
B trees
 
Functions in python3
Functions in python3Functions in python3
Functions in python3
 
Dictionary
DictionaryDictionary
Dictionary
 
Sets
SetsSets
Sets
 
Lists
ListsLists
Lists
 
Data Mining: Data Preprocessing
Data Mining: Data PreprocessingData Mining: Data Preprocessing
Data Mining: Data Preprocessing
 
Dbms
DbmsDbms
Dbms
 

Recently uploaded

AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 

Recently uploaded (20)

AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 

C programs

  • 1. 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;
  • 2. 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;
  • 3. 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 {
  • 4. 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++)
  • 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("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++) {
  • 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 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]) {
  • 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 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; };
  • 12. 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];
  • 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() { 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