pages

Monday, April 27, 2020

C Programming slip 26

C Programming BCA Practical Slips Solution

Slip 26
Q1. Write a C program to calculate length of string without using standard functions.
[15 Marks]
#include <stdio.h>
int main()
{
    char s[1000];
    int i;

    printf("Enter a string: ");
    scanf("%s", s);

    for(i = 0; s[i] != '\0'; ++i);

    printf("Length of string: %d", i);
    return 0;
}

Q2. Write a program to display the elements of an array containing n integers in the
 Reverse order using a pointer to the array. [25 Marks]
#include<stdio.h>
void main() 
{   int size, i, arr[30];
   int *ptr;
    ptr = &arr[0];
   printf("\nEnter the size of array : ");
   scanf("%d", &size);
   printf("\nEnter %d integers into array: ", size);
   for (i = 0; i < size; i++) {
      scanf("%d", ptr);
      ptr++;
   }
   ptr = &arr[size - 1];
   printf("\nElements of array in reverse order are :");
    for (i = size - 1; i >= 0; i--) {
      printf("\nElement%d is %d : ", i, *ptr);
      ptr--;
   }
    getch();
}

No comments:

Post a Comment