Programming Practice In C Language

Que1.

WAP in C Language to print an input string in reverse order.

Code:-

#include <stdio.h>
#include <string.h>
int main()
{
  char str[20];
  int l,i,count=0;
  printf(” Enter Any  Character String “);
  scanf (” %[^\n]”,str);
  printf(“Input String Is: %s “, str);
  printf( “\nReverse String Is: “);
  /* For Reverse String*/
  for (i=0; str[i] != 0 ;i++)
  {
    count++;
  }
  for (i=count;i>=0;i–)
  {
    printf(“%c”, str[i]);
  }
}

Output:

1.1


Que2.

WAP to replace all characters with another character, take the string, character to replace and the character to substitute from the user. e.g. string = ”Hello World”, character to replace = ’o’, replacing character = ’X’, final string = ”HellX WXrld”.

Code:-

#include <stdio.h>
#include <string.h>
int main()
{
  char str[20];
  int counter;
  printf(” Enter Any  Character String “);
  scanf(“%[^/n]”,str);
  printf(“Input String Is: %s”,str);
while(str[counter]!=’\0′)
{
  {
    if ( str[counter] == ‘o’)
    str[counter] = ‘X’;
     }
     counter++;
}
printf(“Replaced String is %s”, str);
return 0;
}

Output:-

1.2


Que.3

 WAP to remove a character from the entire string. e.g. if string is “how are you”, and character ’o’ is removed, final string will be ”hw are yu”.

Code:-

#include <stdio.h>
#include <string.h>
int main()
{
  char str[20];
  int counter=0;
  printf(” Enter Any Character String “);
  scanf(“%[^/n]”,str);
  printf(“Input String Is: %s\n”,str);
for (counter=0;str[counter]!= ‘\0’; counter++)
{if (str[counter]==’o’)
continue;
else
printf(“%c”,str[counter]);
}
return 0;
}
Output:-

1.3


Que.4

 WAP to take an integer from the user and generate all permutations.

Code:-

#include <stdio.h>
#include <string.h> /* Using Library Function Strings*/
void swap(char *x, char *y) /* Swapping The Letters */
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
void permute(char *a, int l, int r) /* Permutation Logic*/
{
   int i;
   if (l == r)
     printf(“%s \n”, a);
   else
   {
       for (i = l; i <= r; i++)
       {
          swap((a+l), (a+i));
          permute(a, l+1, r);
          swap((a+l), (a+i)); /* backtracking */
       }
   }
}
int main()
{
    char str[] = “AB C”;
    int n = strlen(str);
    permute(str, 0, n-1);
    return 0;
}
Output:
2.2

Que:5 WAP  that takes a string from user and prints out if the string is palindrome or not.  Examples of palindrome are ”1213121”,”hannah”.  Palindromes are strings with a property that they read the same forwards and backward.

Code:-

#include<stdio.h>
#include<string.h>

int main()
{
int i,len,flag=0;
char *str;

printf(“\n Enter A String “);
scanf(“%c”, &str);
len=strlen(str);

for(i=0;i<len;i++)
{
if(str[i]==str[len-i-1])
flag=flag+1;
}

if (flag==len){
printf(“\n String is palindrome”); }
else {
printf(“\n String is not palindrome”); }
return 0;

}

3.1


Que6:

Randomly generate  an array of 1000 elements, each element has values from 0 to 100 (0 and 100 inclusive).  You need to write following: a function “float find mean(int* input arrays,  int n)” that takes the array as input and returns mean of all the elements in the array.

Code:-

#include<stdio.h>
#include<stdlib.h> /* For rand() Function */
int main()
{
  int i,r[1000]; /* The Array Of 1000 Elements*/
  float mean;
  int sum=0;
  for(i=0; i<1000; i++)
  {
    r[i] = rand()%101;   /* Choosing 1000 Random Numbers, Under Range Of 0 to 100 */
    sum=sum+r[i];        /* Sum of All Random Numbers */
  }
   printf(” \n Sum Is: %d”,sum);
  mean=sum/1000;  /*  Mean Of Random Numbers */
  printf(” \n Mean Of Randomly Generate Array Is: %f”,mean);

Output:-

4.1


Que7:

Randomly generate  an array of 1000 elements, each element has values from 0 to 100 (0 and 100 inclusive).  You need to write following:

a function  “float find median(int* input array,  int n)” that takes the array as input and returns median  of all the elements  in the array.   For this task  you may need to sort the array.

Code:-

#include<stdio.h>
#include<stdlib.h> /* For rand() Function */
int main()
{
  int i,j,r[1000]; /* The Array Of 1000 Elements*/
  float median;
  int sum=0;
  for(i=0; i<1000; i++)
    r[i] = rand()%101;   /* Choosing 1000 Random Numbers, Under Range Of 0 to 100 */
    /* We Have To Sort The Array Now */
    for (i=0;i<1000;i++)
    {
      for(j=i+1;j<1000;j++)
      {
        if(r[i]>r[j])               /* For Example 98 > 95 */
        {
          r[i]=r[i]+r[j];        /* r[i] = 98+95 = 193   */
          r[j]=r[i]-r[j];       /* r[j] = 193-95 = 98  */
          r[i]=r[i]-r[j];      /* r[i] = 193-98= 95  */
        }
      }
    }
  /* Median is middle element in case of odd elements & if there are even elements then median is mean of two middle one elements */
 /* Here we have 1000 elements hence even elements case is here. */
median=((r[499]+r[500])/2);
printf(“Median Of Random Integers Is %f”,median);
}

Output:-

4.2.JPG


Que8:

 WAP  to multiply matrices  of size m * n and n* p.  Initialize the two matrices in the code itself. (since taking  input  from the user is time-consuming).

Code:-

#include <stdio.h>
int main()
{
  int m, n, p, q, c, d, k, sum = 0;
  int first[100][100], second[100][100], multiply[100][1004
  ];
  /* Declared Memory */
  printf(“Enter the number of rows and columns of first matrix\n”);
  scanf(“%d%d”, &m, &n);
  printf(“Enter the elements of first matrix\n”);
 /* Asking From User To Declare First Matrix Size */
  for (  c = 0 ; c < m ; c++ )
    for ( d = 0 ; d < n ; d++ )
      scanf(“%d”, &first[c][d]);
 /* For First Matrix We Feeding The Values From User */
  printf(“Enter the number of rows and columns of second matrix\n”);
  scanf(“%d%d”, &p, &q);
 /* Feeding Second Matrix Size From User */
  if ( n != p ) /* Checking Basic Matrix Multiplicationn Rule */
    printf(“Matrices with entered orders can’t be multiplied with each other.\n”);
  else
  {
    printf(“Enter the elements of second matrix\n”);
    for ( c = 0 ; c < p ; c++ )
      for ( d = 0 ; d < q ; d++ )
        scanf(“%d”, &second[c][d]);
        /* Logic For Multiplication Matrix */
    for ( c = 0 ; c < m ; c++ ){
      for ( d = 0 ; d < q ; d++ ){
        for ( k = 0 ; k < p ; k++ ){
          sum = sum + first[c][k]*second[k][d];}
        multiply[c][d] = sum;
        sum = 0;}}
    printf(“Product of entered matrices:-\n”);
    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
        printf(“%d\t”, multiply[c][d]);
      printf(“\n”);
  } }
  return 0;
}
Output:-

5.1.JPG



Advertisements