C Slip Answers 21-30
C Slip Answers 21-30
Write a C program to accept a string and convert it into lower case using standard library function
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i;
char str[100];
printf("Enter the string: ");
scanf("%d",&str);
for(i=0;i<strlen(str);i++){
str[i]=tolower(str[i]);
}
printf("String in lower case is: %s",str);
getch();
}
Write a program to accept details of n items (code, name, price) using structure and display the details.
#include<stdio.h>
#include<conio.h>
struct Item{
int code;
char name[50];
int price;
};
void main()
{
int n,i;
struct Item items[n];
printf("Enter the number of items: ");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\nEnter the details of the item %d",i+1);
printf("\nEnter the code of item: ");
scanf("%d",&items[i].code);
printf("\nEnter the name of item: ");
scanf("%s",&items[i].name);
printf("\nEnter the price of item: ");
scanf("%d",&items[i].price);
}
for(i=0;i<n;i++)
{
printf("\nDetails of items are...");
printf("\nItem Code: %d",items[i].code);
printf("\nItem Name: %s",items[i].name);
printf("\nItem Price: %d",items[i].price);
}
getch();
}
Write a program to accept a string and an index from user and displays the character at that specific index.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[100];
int index;
printf("Enter the string: ");
scanf("%s",&str);
printf("Enter the index: ");
scanf("%d",&index);
if(index>=0 && index<strlen(str)){
printf("Character at the index %d: %c",index,str[index]);
}
else
{
printf("Invalid index!!!Index should be between 0 and %d",strlen(str)-1);
}
getch();
}
Write a program to accept details of ‘n’ books (bno, bname, price). Display all the details. Also display all
books having price > 100.
#include<stdio.h>
#include<conio.h>
struct Book{
int bno;
char name[50];
int price;
};
void main()
{
int n,i;
struct Book books[n];
printf("Enter the number of the books: ");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\nEnter the details of book %d",i+1);
printf("\nEnter the number of book: ");
scanf("%d",&books[i].bno);
printf("\nEnter the name of book: ");
scanf("%s",&books[i].name);
printf("\nEnter the price of book: ");
scanf("%d",&books[i].price);
}
printf("\nDetails of books are: ");
for(i=0;i<n;i++){
printf("\nBook Number: %d",books[i].bno);
printf("\nBook Name: %s",books[i].name);
printf("\nBook Price: %d",books[i].price);
}
printf("\n\nBook Name having price greater than 100: ");
for(i=0;i<n;i++){
if(books[i].price>100){
printf("\nBook Name: %s",books[i].name);
printf("\tBook Price: %d",books[i].price);
}
}
getch();
}
Write a program to read the contents of a text file and count the number of lines in the file.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *file;
char c;
int linecount=0;
char filename[100];
printf("Enter the file name: ");
scanf("%s",&filename);
file=fopen(filename,"r");
if(file==NULL){
printf("Could not open the file!!!");
return 1;
}
while((c=fgetc(file))!=EOF){
if(c=='\n'){
linecount++;
}
}
fclose(file);
printf("Number of lines in the file: %d",linecount+1);
getch();
}