1. WAP to read a string from keyboard until a enter key and count vowels, consonants, semicolons.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char inputstring[200];
int len,i=0,countVowel=0,countCons=0,countComma=0,countSemi=0;
clrscr();
printf("\nEnter some test:\n");
gets(inputstring);
len=strlen(inputstring);
for(i=0;i<len;i++)
{
if(inputstring[i]=='a' || inputstring[i]=='e'|| inputstring[i]=='i'||inputstring[i]=='o'||inputstring[i]=='u')
countVowel++;
else if(inputstring[i]==',')
countComma++;
else if(inputstring[i]==';')
countSemi++;
else
countCons++;
}
printf("\nThe numbers of vowels\t%d",countVowel);
printf("\nThe numbers of Consonants\t%d",countCons);
printf("\nThe numbers of Commas\t%d",countComma);
printf("\nThe numbers of Semicolon\t%d",countSemi);
getch();
}
Output:
Enter some test:
My name Is Sujan Kunwar. I am Computer Engineer, Web Designer, Hardware technician, etc;
The numbers of vowels 25
The numbers of Consonants 59
The numbers of Commas 3
The numbers of Semicolon 1
- WAP to define 3 variables of types char, float, int and display memory address reserved by defined variable. Also initialize three pointers with these variables.
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
float b=2.5;
char c='n';
int *p;
float *q;
char *r;
p=&a;
q=&b;
r=&c;
printf("\nThe address of a is %u",p);
printf("\nThe address if b is %u",q);
printf("\nThe address if c is %u",r);
getch();
}
Output:
The address of a is 65524
The address if b is 65520
The address if c is 65519
- WAP to find sum of all elements of one-dimensional array using pointer.
Source Code:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void main()
{
int *a,i,n,result=0;
clrscr();
printf("Enter the no. of elements to be stored in array :");
scanf("%d",&n);
a=(int *)malloc(n*sizeof(int));//allocating space
printf("\nEnter the elements :");
for(i=0;i<n;i++)
scanf("%d",a+i);
for(i=0;i<n;i++)
result+=*(a+i);
printf("\nThe sum of elements in array is :%d",result);
free(a);//deallocating space
getch();
}
Output:
Enter the no. of elements to be stored in array :4
Enter the elements :2 4 6 8
The sum of elements in array is :20
- WAP to sort N numbers (use memory allocation)
Source Code:
#include<stdio.h>
#include<conio.h>
void get(float*,int);
void display(float*,int);
void sort(float*,int);
int main(void)
{
int n;
float *num;
clrscr();
printf("\nEnter numbers of elements in an array:\t");
scanf("%d",&n);
num=(float*)malloc(n*sizeof(float));
get(num,n);
sort(num,n);
display(num,n);
getch();
return 0;
}
void get(float*nums,int n)
{
int i;
printf("\nEnter %d numbers:",n);
for(i=0;i<n;i++)
scanf("%f",nums+i);
}
void sort(float*nums,int n)
{
float temp;
int i,j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(*(nums+j)>*(nums+j))
{
temp=*(nums+i);
*(nums+i)=*(nums+j);
*(nums+j)=temp;
}
}
}
}
void display(float *nums,int n)
{
int i;
printf("\nThe numbers in ascending order:\n");
for(i=0;i<n;i++)
printf("\t%.2f",*(nums+i));
}
Output:
Enter numbers of elements in an array: 8
Enter 8 numbers:12 56 7 90 30 6 54 65
The numbers in ascending order:
12.00 56.00 7.00 90.00 30.00 6.00 54.00 65.00
- WAP to read n numbers in array using pointer concept and display the smallest.
Source Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,n;
int *ptr,*low;
clrscr();
printf("\nEnter the value of n:\t");
scanf("%d",&n);
printf("\nEnter the %d interger value:\t",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
ptr=a;
low=&a[0];
for(i=0;i<n;i++)
{
if(*ptr<*low)
{
low=ptr;
}
ptr++;
}
printf("\nElements of an array:\t");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\nSmallest value is %d\t",*low);
getch();
}
Output:-
Enter the value of n: 5
Enter the 5 integer value: 6 8 10 9 15
Elements of an array: 6 8 10 9 15
Smallest value is 6
- Discuss the following output
#include<stdio.h>
#include<conio.h>
void main()
{
int *x;
int a=20;
*x=10;
*x+=a;
printf("x=%u",x);
printf("\n*x=%d",*x);
printf("\nAddress of x =%u",&x);
getch();
}
Output:
x=36823
*x=30
Address of x =65524
No comments:
Post a Comment