You are on page 1of 12

CODE CAMP DAY1

VJWTSS11190015

1.Write a c program to read a string and print the number of words and
characters in the string.

Program:
/*
*File: c1.c
*Author: U.Ravikiran
*Creator Date: 14/12/2019
*Requirement: #include<stdio.h>
*Description: To read a string and print the number of words and characters in the
string.
*/
#include<stdio.h>
#include<string.h>
int main()
{
char str[100],*p;
int i,word=0,ch=0;
printf("Enter the string:");
gets(str);
p=str;
for(i=0;*p!='\0';i++,p++)
{
if(*p==' ')
{
word++;
ch++;
}
else
ch++;
}
printf("Number of words:%d\n",word+1);
printf("Number of characters:%d\n",ch);
}

Output:

2. Write a c program to check whether a substring present in a given string or


not?

a) Program: without using predefined functions


/*
*File: c2.c
*Author: U.Ravikiran
*Creator Date: 14/12/2019
*Requirement: #include<stdio.h>
*Description: To check whether a substring present in a given string without using
predefined functions
*/
#include<stdio.h>
int main()
{
char str[100], search[10];
int count1 = 0, count2 = 0, i, j, flag;
printf("Enter any string:");
gets(str);
printf("Enter the substring to be searched:");
gets(search);
while(str[count1]!='\0')
count1++;
while(search[count2]!='\0')
count2++;
for(i=0;i<=count1 - count2;i++)
{
for(j=i;j<i + count2;j++)
{
flag = 1;
if(str[j]!=search[j-i])
{
flag = 0;
break;
}
}
if (flag == 1)
break;
}
if (flag == 1)
printf("String found in the main string\n");
else
printf("String not found in the main string\n");
return 0;
}
Output:

b) Program: using predefined functions


/*
*File: c2b.c
*Author: U.Ravikiran
*Creator Date: 14/12/2019
*Requirement: #include<stdio.h>
*Description: To check whether a substring present in a given string or using
predefined functions.
*/

#include<stdio.h>
#include<string.h>
int main()
{
char *p,a[30],b[20];
printf("Enter any string:");
gets(a);
printf("Enter the substring to be searched:");
gets(b);
if(p=strstr(a,b))
{
printf("String found in the main string\n");
}
else
{
printf("String not found in the main string\n");
}
}

Output:

3. Write a C program to find sum of n elements entered by user. To perform


this program, allocate memory dynamically using malloc() function.
Program:
/*
*File: c3.c
*Author: U.Ravikiran
*Creator Date: 14/12/2019
*Requirement: #include<stdio.h>
*Description: To Calculate the sum of n elements entered by the user using
malloc() function
*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,n,*p,sum=0;
printf("Enter number of elements:");
scanf("%d",&n);
p=(int*)malloc(sizeof(int)*n);
printf("Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",p);
sum=sum+*p;
}
printf("Sum is: %d\n",sum);
free(p);
return 0;
}

Output:

4. Find Largest Element Using Dynamic Memory Allocation - calloc()


Program:
/*
*File: c4.c
*Author: U.Ravikiran
*Creator Date: 14/12/2019
*Requirement: #include<stdio.h>
*Description: To Calculate the largest element using dynamic memory allocation –
calloc()
*/
#include<stdio.h>
#include<stdlib.h>
int main(){
int i,n,*p;
printf("Enter number of elements:");
scanf("%d",&n);
p=(int*)calloc(sizeof(int),n);
for(i=0;i<n;i++)
{
printf("Enter the elements:\n");
scanf("%d",(p+i));
}
for(i=0;i<n;i++)
{
if(*p<*(p+i))
*p=*(p+i);
}
printf("Largest number:%d\n",*p);
free(p);
return 0;
}
Output:
5. Create a dynamic list to store customer_no,customer_name and
bill_amount and traverse it to show all the customers.

Program:
/*
*File: c5.c
*Author: U.Ravikiran
*Creator Date: 14/12/2019
*Requirement: #include<stdio.h>
*Description: To store the list of the customer
*/
#include<stdio.h>
#include<stdlib.h>
struct cust
{
int cust_num;
char cust_name[50];
float cust_bill;
struct cust *n;
};
int main()
{
struct cust *st=NULL,*p,*f;
char ch='y';
for( ;ch!='n'; )
{
f=(struct cust*)malloc(sizeof(struct cust));
printf("Enter customer number\n");
scanf("%d",&f->cust_num);
printf("Enter customer name\n");
scanf("%s",f->cust_name);
printf("Enter customer bill amount\n");
scanf("%f",&f->cust_bill);
f->n=NULL;
if(st==NULL)
st=f;
else
{
for(p=st;p->n!=NULL;p=p->n);
p->n=f;
}
getchar();
printf("Enter y to continue n to exit :-");
scanf("%c",&ch);
}
for(p=st;p!=NULL;p=p->n)
{

printf("Enter customer number %d\n",p->cust_num);


printf("Enter customer name %s\n",p->cust_name);
printf("Enter customer bill amount %f\n",p->cust_bill);
}
return 0;
}
Output:
6. Create a function to search a customer in the above list based on the
customer number passed as a parameter to the function.

Program:
/*
*File: c8.c
*Author: U.Ravikiran
*Creator Date: 14/12/2019
*Requirement: #include<stdio.h>
*Description: To search a customer in the given list.
*/
#include<stdio.h>
#include<stdlib.h>
struct cust
{
int cust_num;
char cust_name[50];
float cust_bill;
struct cust *n;
};
int main()
{
struct cust *st=NULL,*p,*f;
char ch='y';
for( ;ch!='n'; )
{
f=(struct cust*)malloc(sizeof(struct cust));
printf("Enter customer number\n");
scanf("%d",&f->cust_num);
printf("Enter customer name\n");
scanf("%s",f->cust_name);
printf("Enter customer bill amount\n");
scanf("%f",&f->cust_bill);
f->n=NULL;
if(st==NULL)
st=f;
else
{
for(p=st;p->n!=NULL;p=p->n);
p->n=f;
}
getchar();
printf("Enter y to continue n to exit:-");
scanf("%c",&ch);
}
for(p=st;p!=NULL;p=p->n)
{

printf("Enter customer number %d\n",p->cust_num);


printf("Enter customer name %s\n",p->cust_name);
printf("Enter customer bill %f\n",p->cust_bill);
}
return 0;
}
Output:

You might also like