pages

Sunday, March 29, 2020

C Programming Slip 6

BCA PRACTICAL SLIPS SOLUTION


Slip6
Q1. Write a C Program to accept a character from the keyboard and display its previous and
next character in order. Ex. If character entered is ‘d’, display “The previous character
is c”, “The next character is e”. [15 Marks]
#include <stdio.h>
int main()
{
char ch;
printf("Enter character:\t");
scanf("%c", &ch);
printf("You entered: %c\n", ch);
printf("Previous character: %c\n", ch - 1);
printf("Next character: %c\n", ch + 1);
}

------------------------------------------------------------------------

Q2. Write a program to accept a string and then count the occurrences of a specific character of
a string. [25 Marks]
#include <stdio.h>
#include <string.h>
 // Driver code
int main()
{ char str[50]= "geeksforgeeks";
 char c ;
 int res = 0;
 printf("\n enter character ");
 scanf("%c",&c);
 for (int i=0;i<strlen(str);i++)
 // checking character in string
 if (str[i] == c)
 res++;
 printf("occurence of %c=%d",c,res++);
 return 0;
}

No comments:

Post a Comment