The document contains questions and program code related to C programming and data structures. Some key points:
- There are questions on sorting arrays, checking for loops in arrays, nested structures, sorting based on dates of birth, and implementing functions for book-author-shelf structures.
- Program code is provided as solutions to these questions, making use of structures, pointers, functions, and linked lists.
- One question asks to write a function to sort records in a linked list based on social security number, and code for creating, displaying, and sorting a linked list is given.
Questions has 4 parts.1st part Program to implement sorting algor.pdfapexelectronices01
Questions has 4 parts.
1st part: Program to implement sorting algorithms:
#include
#include
#include
using namespace std;
void swap(std::vector & data, int i, int j)
{
int tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
void print(std::vector const & data)
{
std::vector::const_iterator iter = data.begin();
for (; iter != data.end(); ++iter)
{
cout << *iter << \" \";
}
if (data.size() > 0)
{
cout << endl;
}
}
int generateRandom(int low, int high);
void Shuffle(std::vector & data)
{
int length = data.size();
for (int i = 0; i < length-1; ++i)
{
swap(data, i, generateRandom(i+1, length-1));
}
print(data);
}
int generateRandom(int low, int high)
{
srand(low);
int gen = 0;
gen = rand() % (high - low + 1) + low;
return gen;
}
//useful for small lists, and for large lists where data is
//already sorted
void BubbleSort(std::vector & data)
{
int length = data.size();
for (int i = 0; i < length; ++i)
{
bool swapped = false;
for (int j = 0; j < length - (i+1); ++j)
{
if (data[j] > data[j+1])
{
swap(data, j, j+1);
swapped = true;
}
}
if (!swapped) break;
}
}
//useful for small lists and where swapping is expensive
// does at most n swaps
void SelectionSort(std::vector & data)
{
int length = data.size();
for (int i = 0; i < length; ++i)
{
int min = i;
for (int j = i+1; j < length; ++j)
{
if (data[j] < data[min])
{
min = j;
}
}
if (min != i)
{
swap(data, i, min);
}
}
}
//useful for small and mostly sorted lists
//expensive to move array elements
void InsertionSort(std::vector & data)
{
int length = data.size();
for (int i = 1; i < length; ++i)
{
bool inplace = true;
int j = 0;
for (; j < i; ++j)
{
if (data[i] < data[j])
{
inplace = false;
break;
}
}
if (!inplace)
{
int save = data[i];
for (int k = i; k > j; --k)
{
data[k] = data[k-1];
}
data[j] = save;
}
}
}
void Merge(std::vector & data, int lowl, int highl, int lowr, int highr);
void MergeSort(std::vector & data, int low, int high)
{
if (low >= high)
{
return;
}
int mid = low + (high-low)/2;
MergeSort(data, low, mid);
MergeSort(data, mid+1, high);
Merge(data, low, mid, mid+1, high);
}
void Merge(std::vector & data, int lowl, int highl, int lowr, int highr)
{
int tmp_low = lowl;
std::vector tmp;
while (lowl <= highl && lowr <= highr)
{
if (data[lowl] < data[lowr])
{
tmp.push_back(data[lowl++]);
}
else if (data[lowr] < data[lowl])
{
tmp.push_back(data[lowr++]);
}
else
{
tmp.push_back(data[lowl++]);
tmp.push_back(data[lowr++]);
}
}
while (lowl <= highl)
{
tmp.push_back(data[lowl++]);
}
while (lowr <= highr)
{
tmp.push_back(data[lowr++]);
}
std::vector::const_iterator iter = tmp.begin();
for(; iter != tmp.end(); ++iter)
{
data[tmp_low++] = *iter;
}
}
int Partition(std::vector & data, int low, int high);
void QuickSort(std::vector & data, int low, int high)
{
if (low >= high) return;
int p = Partition(data, low, high);
QuickSort(data, low, p-1);
QuickSort(data, p+1, high);
}
int Partition(std::vector & data, int low, int high)
{
int p = low;
for (int i = p+1; i <= high; ++i)
{
if (data[i] < data[p])
{
swap.
Questions has 4 parts.1st part Program to implement sorting algor.pdfapexelectronices01
Questions has 4 parts.
1st part: Program to implement sorting algorithms:
#include
#include
#include
using namespace std;
void swap(std::vector & data, int i, int j)
{
int tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
void print(std::vector const & data)
{
std::vector::const_iterator iter = data.begin();
for (; iter != data.end(); ++iter)
{
cout << *iter << \" \";
}
if (data.size() > 0)
{
cout << endl;
}
}
int generateRandom(int low, int high);
void Shuffle(std::vector & data)
{
int length = data.size();
for (int i = 0; i < length-1; ++i)
{
swap(data, i, generateRandom(i+1, length-1));
}
print(data);
}
int generateRandom(int low, int high)
{
srand(low);
int gen = 0;
gen = rand() % (high - low + 1) + low;
return gen;
}
//useful for small lists, and for large lists where data is
//already sorted
void BubbleSort(std::vector & data)
{
int length = data.size();
for (int i = 0; i < length; ++i)
{
bool swapped = false;
for (int j = 0; j < length - (i+1); ++j)
{
if (data[j] > data[j+1])
{
swap(data, j, j+1);
swapped = true;
}
}
if (!swapped) break;
}
}
//useful for small lists and where swapping is expensive
// does at most n swaps
void SelectionSort(std::vector & data)
{
int length = data.size();
for (int i = 0; i < length; ++i)
{
int min = i;
for (int j = i+1; j < length; ++j)
{
if (data[j] < data[min])
{
min = j;
}
}
if (min != i)
{
swap(data, i, min);
}
}
}
//useful for small and mostly sorted lists
//expensive to move array elements
void InsertionSort(std::vector & data)
{
int length = data.size();
for (int i = 1; i < length; ++i)
{
bool inplace = true;
int j = 0;
for (; j < i; ++j)
{
if (data[i] < data[j])
{
inplace = false;
break;
}
}
if (!inplace)
{
int save = data[i];
for (int k = i; k > j; --k)
{
data[k] = data[k-1];
}
data[j] = save;
}
}
}
void Merge(std::vector & data, int lowl, int highl, int lowr, int highr);
void MergeSort(std::vector & data, int low, int high)
{
if (low >= high)
{
return;
}
int mid = low + (high-low)/2;
MergeSort(data, low, mid);
MergeSort(data, mid+1, high);
Merge(data, low, mid, mid+1, high);
}
void Merge(std::vector & data, int lowl, int highl, int lowr, int highr)
{
int tmp_low = lowl;
std::vector tmp;
while (lowl <= highl && lowr <= highr)
{
if (data[lowl] < data[lowr])
{
tmp.push_back(data[lowl++]);
}
else if (data[lowr] < data[lowl])
{
tmp.push_back(data[lowr++]);
}
else
{
tmp.push_back(data[lowl++]);
tmp.push_back(data[lowr++]);
}
}
while (lowl <= highl)
{
tmp.push_back(data[lowl++]);
}
while (lowr <= highr)
{
tmp.push_back(data[lowr++]);
}
std::vector::const_iterator iter = tmp.begin();
for(; iter != tmp.end(); ++iter)
{
data[tmp_low++] = *iter;
}
}
int Partition(std::vector & data, int low, int high);
void QuickSort(std::vector & data, int low, int high)
{
if (low >= high) return;
int p = Partition(data, low, high);
QuickSort(data, low, p-1);
QuickSort(data, p+1, high);
}
int Partition(std::vector & data, int low, int high)
{
int p = low;
for (int i = p+1; i <= high; ++i)
{
if (data[i] < data[p])
{
swap.
Programming Fundamentals Arrays and Strings imtiazalijoono
Programming Fundamentals
Arrays and Strings
• Arrays
• Initializing arrays
• Multidimensional arrays
• Arrays as arguments to functions
• Strings
• String functions
Accessing Individual Components
Declaring Arrays
Arrays: Example Garbage
Multidimensional Arrays
1) Write a C program using arrays that produces the multiplication of two 2x2 matrices.
3-Dimensional Array
Write a program of your own choice that makes use of arrays of more than 2 dimensions.
2-Dimensional Arrays
Let us C (by yashvant Kanetkar) chapter 3 SolutionHazrat Bilal
All rights reserved. No part of this slide
may be reproduced, stored in a retrieval
system, or transmitted, in any form or by
any means, without the prior permission.
it is and slide contains Save to your mobile device with our new app
Don't have time to read now? Save SlideShares to read later or offline - anytime, anywhere.
dding more information (description, tags, category) makes it easier for others to find your content. The score increases as you add each item.
C program to find factorial of number using recursion as well as iteration ,
Calculate power of a number program in c using Recursion and Iteration, Write a C program to count digits of a number using Recursion and Iteration, Write a C program to find sum of first n natural numbers using Recursion, C program to print sum of digits of a given number using recursion ,Write a C program to find nth term in Fibonacci Series using Recursion, C program to find out the GCD (Greatest Common Divisor )of the two numbers using recursion,
Write a C program to find the first upper case letter in the given string using recursion, write C program to calculate length of the string using Recursion ,
Write a program in C to count number of divisors of a given number using recursion, Recursive program to check whether a given number is prime or composite,
C program to displays integers 100 through 1 using Recursion and Iteration, Write a program in C to convert a decimal number to binary using recursion,
Recursion Stack of factorial of 3 Recursion stack of 4th term of Fibonacci
Complete DBMS notes..with special attention to SQL commands and advanced SQL commands, Transaction management. The below post is notes prepared by me by studying the book "Database Systems Design, Implementation and Management" by Peter Rob and Carlos Coronel
Content, examples and diagrams are taken from that book.
DevOps and Testing slides at DASA ConnectKari Kakkonen
My and Rik Marselis slides at 30.5.2024 DASA Connect conference. We discuss about what is testing, then what is agile testing and finally what is Testing in DevOps. Finally we had lovely workshop with the participants trying to find out different ways to think about quality and testing in different parts of the DevOps infinity loop.
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf91mobiles
91mobiles recently conducted a Smart TV Buyer Insights Survey in which we asked over 3,000 respondents about the TV they own, aspects they look at on a new TV, and their TV buying preferences.
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Tobias Schneck
As AI technology is pushing into IT I was wondering myself, as an “infrastructure container kubernetes guy”, how get this fancy AI technology get managed from an infrastructure operational view? Is it possible to apply our lovely cloud native principals as well? What benefit’s both technologies could bring to each other?
Let me take this questions and provide you a short journey through existing deployment models and use cases for AI software. On practical examples, we discuss what cloud/on-premise strategy we may need for applying it to our own infrastructure to get it to work from an enterprise perspective. I want to give an overview about infrastructure requirements and technologies, what could be beneficial or limiting your AI use cases in an enterprise environment. An interactive Demo will give you some insides, what approaches I got already working for real.
UiPath Test Automation using UiPath Test Suite series, part 3DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 3. In this session, we will cover desktop automation along with UI automation.
Topics covered:
UI automation Introduction,
UI automation Sample
Desktop automation flow
Pradeep Chinnala, Senior Consultant Automation Developer @WonderBotz and UiPath MVP
Deepak Rai, Automation Practice Lead, Boundaryless Group and UiPath MVP
GraphRAG is All You need? LLM & Knowledge GraphGuy Korland
Guy Korland, CEO and Co-founder of FalkorDB, will review two articles on the integration of language models with knowledge graphs.
1. Unifying Large Language Models and Knowledge Graphs: A Roadmap.
https://arxiv.org/abs/2306.08302
2. Microsoft Research's GraphRAG paper and a review paper on various uses of knowledge graphs:
https://www.microsoft.com/en-us/research/blog/graphrag-unlocking-llm-discovery-on-narrative-private-data/
Securing your Kubernetes cluster_ a step-by-step guide to success !KatiaHIMEUR1
Today, after several years of existence, an extremely active community and an ultra-dynamic ecosystem, Kubernetes has established itself as the de facto standard in container orchestration. Thanks to a wide range of managed services, it has never been so easy to set up a ready-to-use Kubernetes cluster.
However, this ease of use means that the subject of security in Kubernetes is often left for later, or even neglected. This exposes companies to significant risks.
In this talk, I'll show you step-by-step how to secure your Kubernetes cluster for greater peace of mind and reliability.
Key Trends Shaping the Future of Infrastructure.pdfCheryl Hung
Keynote at DIGIT West Expo, Glasgow on 29 May 2024.
Cheryl Hung, ochery.com
Sr Director, Infrastructure Ecosystem, Arm.
The key trends across hardware, cloud and open-source; exploring how these areas are likely to mature and develop over the short and long-term, and then considering how organisations can position themselves to adapt and thrive.
Essentials of Automations: Optimizing FME Workflows with ParametersSafe Software
Are you looking to streamline your workflows and boost your projects’ efficiency? Do you find yourself searching for ways to add flexibility and control over your FME workflows? If so, you’re in the right place.
Join us for an insightful dive into the world of FME parameters, a critical element in optimizing workflow efficiency. This webinar marks the beginning of our three-part “Essentials of Automation” series. This first webinar is designed to equip you with the knowledge and skills to utilize parameters effectively: enhancing the flexibility, maintainability, and user control of your FME projects.
Here’s what you’ll gain:
- Essentials of FME Parameters: Understand the pivotal role of parameters, including Reader/Writer, Transformer, User, and FME Flow categories. Discover how they are the key to unlocking automation and optimization within your workflows.
- Practical Applications in FME Form: Delve into key user parameter types including choice, connections, and file URLs. Allow users to control how a workflow runs, making your workflows more reusable. Learn to import values and deliver the best user experience for your workflows while enhancing accuracy.
- Optimization Strategies in FME Flow: Explore the creation and strategic deployment of parameters in FME Flow, including the use of deployment and geometry parameters, to maximize workflow efficiency.
- Pro Tips for Success: Gain insights on parameterizing connections and leveraging new features like Conditional Visibility for clarity and simplicity.
We’ll wrap up with a glimpse into future webinars, followed by a Q&A session to address your specific questions surrounding this topic.
Don’t miss this opportunity to elevate your FME expertise and drive your projects to new heights of efficiency.
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...James Anderson
Effective Application Security in Software Delivery lifecycle using Deployment Firewall and DBOM
The modern software delivery process (or the CI/CD process) includes many tools, distributed teams, open-source code, and cloud platforms. Constant focus on speed to release software to market, along with the traditional slow and manual security checks has caused gaps in continuous security as an important piece in the software supply chain. Today organizations feel more susceptible to external and internal cyber threats due to the vast attack surface in their applications supply chain and the lack of end-to-end governance and risk management.
The software team must secure its software delivery process to avoid vulnerability and security breaches. This needs to be achieved with existing tool chains and without extensive rework of the delivery processes. This talk will present strategies and techniques for providing visibility into the true risk of the existing vulnerabilities, preventing the introduction of security issues in the software, resolving vulnerabilities in production environments quickly, and capturing the deployment bill of materials (DBOM).
Speakers:
Bob Boule
Robert Boule is a technology enthusiast with PASSION for technology and making things work along with a knack for helping others understand how things work. He comes with around 20 years of solution engineering experience in application security, software continuous delivery, and SaaS platforms. He is known for his dynamic presentations in CI/CD and application security integrated in software delivery lifecycle.
Gopinath Rebala
Gopinath Rebala is the CTO of OpsMx, where he has overall responsibility for the machine learning and data processing architectures for Secure Software Delivery. Gopi also has a strong connection with our customers, leading design and architecture for strategic implementations. Gopi is a frequent speaker and well-known leader in continuous delivery and integrating security into software delivery.
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];