0% found this document useful (0 votes)
24 views

Name - Ayush Raj REG NO - 20BCT0168 SUBMISSION DATE - 27/02/2022

The document contains a student's submission for an Advanced C Lab assignment. It includes 10 questions on pointers and structures with the student's code solutions. The code covers topics like finding the largest number, summing array elements, swapping variables, counting characters and words, string manipulation, structs to store student data, reversing numbers, factorials, analyzing text with pointers to strings, and counting days between dates.

Uploaded by

Ayush Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Name - Ayush Raj REG NO - 20BCT0168 SUBMISSION DATE - 27/02/2022

The document contains a student's submission for an Advanced C Lab assignment. It includes 10 questions on pointers and structures with the student's code solutions. The code covers topics like finding the largest number, summing array elements, swapping variables, counting characters and words, string manipulation, structs to store student data, reversing numbers, factorials, analyzing text with pointers to strings, and counting days between dates.

Uploaded by

Ayush Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

School of Computer Science and Engineering

CSE2010-Advance C Lab
Semester: Winter 2021-22
Slot: L25+L26

NAME – AYUSH RAJ


REG NO – 20BCT0168
SUBMISSION DATE – 27/02/2022
Question and Answer –

(Pointers and Structures, Memory Allocation:


Pointer and one dimensional arrays, Array of
pointers, Pointers and two dimensional arrays,
Subscripting pointer to an array, Dynamic 1D and
2D array.)
1.
Write a program to find biggest among three
numbers using pointer.
#include <stdio.h>
int main(){
int num1, num2, num3;
int *p1, *p2, *p3;

printf("Enter First Number : ");


scanf("%d", &num1);
printf("Enter Second Number : ");
scanf("%d", &num2);
printf("Enter Third Number : ");
scanf("%d", &num3);

// assigning the address of input numbers to pointers


p1 = &num1;
p2 = &num2;
p3 = &num3;
if (*p1 > *p2){
if (*p1 > *p3){
printf("%d is the largest number", *p1);
}
else{
printf("%d is the largest number", *p3);
}
}
else{
if (*p2 > *p3){
printf("%d is the largest number", *p2);
}
else{
printf("%d is the largest number", *p3);
}
}
return 0;
}
Output -
2.
Write a program to find the sum of all the elements of an array
using pointers.
#include <stdio.h>
#include <stdlib.h>
int main(){
int n, sum = 0;
printf("Enter the Number of Elements in the Array : ");
scanf("%d", &n);
int *arr = (int *)malloc(n * sizeof(int));
for (int i = 0; i < n; i++){
printf("Enter the element at position [%d] : ", i);
scanf("%d", &*(arr + i));
sum += *(arr + i);
}
printf("\nSum of all the elements of the Array is : %d", sum);
return 0;
}

Output –
3.
Write a program to swap value of two variables using pointer.

#include <stdio.h>
void swap(int *a, int *b){
int c = *a;
*a = *b;
*b = c;
}
int main(){
int a, b;
printf("Enter the Two Numbers for Swapping :- ");
scanf("%d %d", &a, &b);
swap(&a, &b);

printf("\nElements after Swapping :- %d %d", a, b);


return 0;
}

Output –
4.
Write a program to read a sentence and count the
number of characters &words in that
sentence.
#include <stdio.h>
#include <stdlib.h>
int main() {
char *str = (char *) malloc(100 * sizeof(char));
int i=0, word=0, chr=0;
printf("\nEnter Your String: ");
gets(str);
while (str[i] != '\0') {
if (str[i] == ' '){
word++;
chr++;
}
else
chr++;
i++;
}
printf("\nNumber of characters: %d", chr);
printf("\nNumber of words: %d", word+1);
}

Output –
5.
Write a program to read a sentence & delete all the
white spaces. Replace all “.” by “:”.

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main(){
char *s;
s = (char *) malloc(1000 * sizeof(char));
int i,k=0;
printf("Enter the string : ");
gets(s);
printf("string after replacing all {.} by {:} and removing all white
spaces \n");
for(i=0; i<strlen(s); i++){
if(*(s+i)=='.'){
*(s+i)=':';
printf("%c", *(s+i));
}
else if(*(s+i)==' '){
continue;
}
else
printf("%c", *(s+i));
}
return 0;
}
Output –
6.
Write a program to read RollNo, Name, Address, Age &
marks in physics, C, math in 1st semester of three
students in B.Tech and display the student details with
average marks achieved.(use structures)

#include<stdio.h>
#include<stdlib.h>
struct student{
int roll,age,phy,c,math;
char name[20],address[50];
float avg;
};
int main(){
struct student *ptr;
int n = 3;
ptr = (struct student *)malloc(n*sizeof(struct student));
for(int i=0;i<n;++i){
printf("Enter RollNo,Name, Address, Age & marks in physics,C, math in 1
st semester of student %d in B.Tech\n",i+1);
scanf("%d %s %s %d %d %d %d",
&(ptr+i)->roll,
&(ptr+i)->name,
&(ptr+i)->address,
&(ptr+i)->age
,&(ptr+i)->phy,
&(ptr+i)->c,
&(ptr+i)->math
);
(ptr+i)->avg = (float)((ptr+i)->phy + (ptr+i)->c + (ptr+i)->math)/3;
}
for(int i=0;i<n;++i){
printf("Details for Student %d:\nRollNo: %d\nName:%s\nAddress:
%s\nAge: %d\nAverage Marks achieved in sem 1: %.2f\n",i+1,
(ptr+i)->roll,(ptr+i)->name,(ptr+i)->address,(ptr+i)->age,(ptr+i)-
>avg);
}
return 0;
}
Output –
7.
Reversing a number using pointer .
#include<stdio.h>
#include<stdlib.h>
int rev(int *a){
int num = 0;
while (*a!=0){
num = num*10 + *a%10;
*a = *a/10;
}
return num;
}
int main(){
int n;
printf("Enter a number: ");
scanf("%d",&n);
printf("Reverse of the number is %d",rev(&n));
}

Output –
8.
Factorial of a number using the pointers .

#include<stdio.h>
int fact(int* num) {
int n1 = *num;
if (n1>=1){
*num -= 1;
return n1*fact(num);
}
else
return 1;
}
int main() {
int n2 = 0;
int *n = &n2;
printf("\nEnter an integer: ");
scanf("%d",n);
printf("Factorial of the number is = %d",fact(n));
return 0;
}

Output –
9.
Write a c program to read multiple lines of a text as
individual strings who's max length is unspecified
maintain a pointer to each string within a one
dimensional array of pointers then determine the no. of
vowels consonants, digits ,white spaces and other
characters of each line finally determine the avg no.
Vowels per line and consonants per line.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char str1[150], str2[150], str3[150];
char* str[3];
str[0] = &str1;
str[1] = &str2;
str[2] = &str3;

int vowels, consonant, digit, space, other;


vowels = consonant = digit = space = other = 0;
double sumv = 0, avgv = 0, sumc = 0, avgc = 0;
printf("Enter lines of string: \n");
for (int i = 0; i < 3; i++){
fgets(str[i], sizeof(str1), stdin);
}
for (int j = 0; j < 3; j++){
for (int i = 0; *(str[j]+i) != '\0'; ++i) {

*(str[j]+i) = tolower(*(str[j]+i));

// Vowel Check
if (*(str[j]+i) == 'a' || *(str[j]+i) == 'e' || *(str[j]+i) == 'i'
|| *(str[j]+i) == 'o' || *(str[j]+i) == 'u') {
++vowels;
}

// Consonant Check
else if ((*(str[j]+i) >= 'a' && *(str[j]+i) <= 'z')) {
++consonant;
}
// Digits Check
else if (*(str[j]+i) >= '0' && *(str[j]+i) <= '9') {
++digit;
}

// Empty Space Check


else if (*(str[j]+i) == ' ') {
++space;
}
}
int sum = vowels+consonant+digit+space;
other = strlen((str[j]))-sum-1;
printf("\nLine %d Details : ", (j+1));
printf("\nVowels\t: %d", vowels);
printf("\nConsonants\t: %d", consonant);
printf("\nDigits\t: %d", digit);
printf("\nWhite Spaces\t: %d", space);
printf("\nOther Chars\t: %d\n", other);
sumv += vowels;
sumc += consonant;
vowels = consonant = digit = space = other = 0;
}
avgc = sumc/3;
avgv = sumv/3;
printf("\nAverage no. of consonants \t= %.2f", avgc);
printf("\nAverage no. of vowels \t= %.2f", avgv);
return 0;
}
Output –
10.
Counting total number of days –

#include<stdio.h>
#include<stdlib.h>

int isLeapYear(int year){


if (year % 400 == 0)
return 1;
else if (year % 100 == 0)
return 0;
else if (year % 4 == 0)
return 1;
else
return 0;
}

int monthDays(int m, int y){


// int m = *mon;
if(m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12 ){
return 31;
}
else if(m == 2){
if(isLeapYear(y) == 1){
return 29;
}
return 28;
}
else{
return 30;
}
}

int main(){
int day1, month1, year1;
int day2, month2, year2;
int d = 0;
int* days = &d;
int w = 1, x;
printf("Enter start date (DD/MM/YYYY)\t: ");
scanf("%d %d %d", &day1, &month1, &year1);

printf("Enter end date (DD/MM/YYYY)\t: ");


scanf("%d %d %d", &day2, &month2, &year2);
x = monthDays(month1, year1);
*days = x - day1;
while(w){
(month1) += 1;
if(month1 > 12){
year1 += 1;
month1 = (month1)%12;
if(year1 == year2){
if(month2 == month1){
x = monthDays(month1, year1);
*days += day2;
break;
}
else{
x = monthDays(month1, year1);
*days += x;
continue;
}
}
}
if(year1 == year2 && month2 == month1){
*days += day2;
break;
}
else if(month1 != month2){
int x = monthDays(month1, year1);
*days += x;
continue;
}
else if(year1 != year2){
int x = monthDays(month1, year1);
*days += x;
continue;
}
}
printf("The no. of days are : %d", *days);
}
Output –

You might also like