C Program Page 6
To view other topics related to Computer. Click Here.
Write a program to read a sentence and count a specific letter (LOOP)
main()
{
char s[30],ch;
int c,l,t=0;
printf(“Enter letter to counted\n”);
ch =getche();
l = strlen( s);
for(i=0 ;i<=l ;I ++)
{
if(s[i] = =c)
t++;
}
printf(“%c comes %d times “,ch ,t);
}
Read a sentence and count vowel ,uppercase to lower case
main()
{
char s[20];
int v, l , u ,l g;
gets (s);
lg= strlen (s)
for (i=0; i<=lg ;i++)
{
if (s [i]>= ‘A’ && s[i]<= ‘Z’)
u++;
if( s[i]>=’a’ && s[i]<=’z’)
l++;
if (s [i] ==’a’)II s[i]==’z’) II( s[i] == ‘I’) II(s[i] ==’o’) II(s[i] = =’u’)
v++;
}
printf (“Total upper case letter = %d”, u);
printf(“Total lower case letter = %d”, u);
printf(“Total vowel case letter =%d “,u);
}
Program to show use of char name
main()
{
char name[25];
printf” Enter your name\n”);
scanf(“Hello %s !”, name );
}
Program to display a string
main()
{
char name[ ]=”Wasim javed “;
int l;
for(i=0 ;i<=7 ;i++)
printf (“%c”, name [i]);
}
Sorting in Descending order
main()
{
int l[30],I, j, t;
for(i=1 ;i<=10;i++)
{
printf(Enter a number\n”);
scanf(“%d,&l[l]);
}
for(i=1 <10;i++)
if( j=l+;j<=10;j++)
if(l[i]>l[j=1])
{
t=l[i];
l[i]=l[i];
l[j+1]=t;
}
printf(sorted list\n”);
for(i=1 ;i<=10;j++)
printf(“%d\n”,l[i]);
}
Sorting in ascending order
main()
{
int l [30]; I; j;t;
for(i=l; i<=10; i++)
{
printf(“Enter a number \n”);
scanf(“%d”,&l[l]);
}
for(i=l; i<10; i++)
for(j=l+1; j<=10 ;j++)
if( l[l]>l[j])
{
t=l[i];
l[i]=l[i];
l[i]=t;
}
printf(“sorted list \n”);
for(i=1; i<=10; i++)
printf(“%d\n”,l[i]);
}
Binary Search
main()
{
int a[10] ,f=0,l=0 ,loc=0, n,l,md;
printf(Enter the data in sorted order\n”);
for(l=0 ; l <10 ;l++)
{
printf((Enter no\n”);
scanf(“%d”,&a[l]);
printf(Enter no to be searched \n”);
scanf(“%d”,&n);
while(f{
mid=f+l)/2;
if(a[mid]==n);
{
loc=mid+1;
break;
}
if(a[mid]>n)
l=mid-1;
if(a[mid]f=mid+1;
}
jif(loc>=0);
printf(element is not found\n”);
else
printf(“element at location=%d\n”loc);
}
Bubble sort
main()
{
int a[50],l,j,t,n;
pritnf(“Enter total element to be sorted\n”);
scanf(“%d”,&n);
for(l=0;l{
printf(“enter a number\n”);
scanf(“%d”,&a[l]);
}
for(l=0; lfor(j=0 ;jif(a[j]< a[a+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
printf(sorted list\n”);
for(l=0; lprintf(“%d\n”,a[j]);
getch();
}
Example of linear or sequential search
main()
{
inta[10],sr,I,loc=0, f=0;
for(i=0, i<=10,i++)
{
printf(“Enter the number\n”);
scabf(“%d”,&a[i]);
}
printf(Enter the number to search\n”);
scanf(“%d”,&sr);
i=0;
while(i<10&&f==0)
{
if(sr==a[i])
{
f=1;
loc=i+1;
}
else
i++;
}
if(f==1)
printf(“Element is found at location=%d\n”,loc);
else
printf(Element is not found\n”);
getch();
}
}
Post a Comment