Name - Ayush Raj REG NO - 20BCT0168 SUBMISSION DATE - 27/02/2022
Name - Ayush Raj REG NO - 20BCT0168 SUBMISSION DATE - 27/02/2022
CSE2010-Advance C Lab
Semester: Winter 2021-22
Slot: L25+L26
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);
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;
*(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;
}
#include<stdio.h>
#include<stdlib.h>
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);