EXERCISE-12

1. Write a program in C to swap elements using call by reference.
SOURCE CODE:
#include<stdio.h>
int main() {
int num1, num2;
printf("Enter two integer values : ");
scanf("%d %d", &num1, &num2);
printf("Before swapping in main : num1 = %d \t num2 = %d\n", num1, num2);
swap(&num1, &num2);
printf("After swapping in main : num1 = %d \t num2 = %d\n", num1, num2);
return 0;
}

void swap(int *num1, int *num2){
    int temp;
    temp = *num1;
    *num1 = *num2;
    *num2 = temp;
    printf("After swapping in swap : *num1 = %d \t *num2 = %d\n", *num1, *num2);
}
OUTPUT:
Enter two integer values : 5 6
Before swapping in main : num1 = 5       num2 = 6
After swapping in swap : *num1 = 6       *num2 = 5
After swapping in main : num1 = 6        num2 = 5

2. Write a program in C to count the number of vowels and consonants in a string using a pointer.
SOURCE CODE:
#include<stdio.h>
int main(){
char str[100], *ch;
int i, v=0, c=0;
printf("Enter a string: ");
scanf("%s",str);
ch = str;
while(*ch!='\0'){
if(*ch=='a' || *ch=='e' || *ch=='i' || *ch=='o' || *ch=='u' || *ch=='A' || *ch=='E' || *ch=='I' || *ch=='O' || *ch=='U'){
v++;
}
else{
c++;
}
ch++;
}
printf("Total number of vowels : %d, consonants : %d\n",v,c);
return 0;
}
OUTPUT:
Enter a string: welcometocprogramming
Total number of vowels : 7, consonants : 14

No comments:

Post a Comment

Total Pageviews