Important Program of C language in O level exam january 2019
Important Program of C language in O level exam january 2019 :-
Write a C Program to count number of words in a string.
#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
char str[MAX_SIZE];
int i,
words;
/* Input
string from user */
printf("Enter any string: ");
gets(str);
i = 0;
words = 1;
/* Runs a
loop till end of string */
while(str[i]
!= '\0')
{
/* If
the current character(str[i]) is white space */
if(str[i]=='
' || str[i]=='\n' || str[i]=='\t')
{
words++;
}
i++;
}
printf("Total number of words = %d",
words);
return 0;
}
Write a ‘C’ program to list of keywords which must be sorted in increasing order in the table.
#include<stdio.h>
int power(int,int);
void main()
{
char keyword[][15]={"char","do","extern","if","return","static","union","while","case","default","enum","goto","register","
"typedef","volatile","break","continue","else","for","long","signed","switch","void","auto","const","double",
"float","int","short","struct","unsigned"};
int i,j;
char tmp[15];
printf("\nUnsorted Keyword Array\n\n");
for(i=0;i<32;i++)
printf("[%s] ",keyword[i]);
for(i=0;i<32;i++)
{
for(j=0;j<32-i-1;j++)
{
if(strcmp(keyword[j],keyword[j+1])>0)
{
strcpy(tmp,keyword[j]);
strcpy(keyword[j],keyword[j+1]);
strcpy(keyword[j+1],tmp);
}
}
}
printf("\n\nSorted Keyword Array\n\n");
for(i=0;i<32;i++)
printf("[%s] ",keyword[i]);
}
Write a C program to write a line of string at a text file.
#include <stdio.h>
#include <stdlib.h> /* For exit() function */
int
main()
{
char sentence[1000];
FILE *fptr;
fptr = fopen("program.txt", "w");
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter a sentence:\n");
gets(sentence);
fprintf(fptr,"%s", sentence);
fclose(fptr);
return 0;
}
Write a C program to find the power of a number using
function
#include<stdio.h>
void
calculate_power(int,int);
void
main()
{
int
b,e;
printf("Enter
the base\n");
scanf("%d",&b);
printf("Enter
the exponent\n");
scanf("%d",&e);
calculate_power(b,e);
}
void
calculate_power(int b,int e)
{
int
power=1;
while(e>0)
{
power=power*b;
e--;
}
printf("The
power of the no = %d",power);
}
|
Write a C program that displays the recommended actions depending on the color of a traffic light using the switch statement.
void main (void)
{
char colour;
/* ask
user for colour */
printf ("Enter
the colour of the light (R,G,Y,A): ");
scanf ("%c",
&colour);
/* test
the alternatives */
switch (colour)
{
/* red light */
case 'R':
case 'r':
printf ("STOP! \n");
break;
/* yellow or amber light */
case 'Y':
case 'y':
case 'A':
case 'a':
printf ("CAUTION! \n");
break;
/* green light */
case 'G':
case 'g':
printf ("GO! \n");
break;
/* other colour */
default:
printf ("The colour is not valid.\n");
Write a C program to find size of structure without using size of operator
struct mystruct{
int a;
float b;
char c;
};
int main(){
struct mystruct *ptr=(struct mystrcut *)0;
ptr++;
printf("Size of structure is: %d",*ptr);
return 0;
}
Dangling Pointer with example
A pointer pointing to a memory
location that has been deleted (or freed) is called dangling pointer.
Wild Pointer with example
A pointer which has not been
initialized to anything (not even NULL) is known as wild pointer. The pointer
may be initialized to a non-NULL garbage value that may not be a valid address.
int main()
{
int
*p; /* wild pointer */
int
x = 10;
//
p is not a wild pointer now
p
= &x;
return
0;
}
If this article is helpful to you then please like share and comments .. thank you
Comments
Post a Comment