Tuesday, December 17, 2019

Write a C program to append the contents of one file at the end of another file

#include <stdio.h>
#include <stdlib.h>
int main()
{
      FILE *fp1, *fp2;
      char fname1[50],fname2[50],c;
      printf("Enter filename to open for reading : ");
      scanf("%s", fname1);
      // Open one file for reading
      fp1 = fopen(fname1, "r");
      if (fp1 == NULL)
     {
            printf("%s file does not exist..", fname1);
            exit(0);
      }
      printf("\nEnter filename to append the content : ");
      scanf("%s", fname2);
      // Open another file for appending content
      fp2 = fopen(fname2, "a");
      if (fp2 == NULL)
      {
            printf("%s file does not exist...", fname2);
            exit(0);
      }
      // Read content from file
      c = fgetc(fp1);
      while (c != EOF)
      {
            fputc(c,fp2);
            c = fgetc(fp1);
      }
      printf("\nContent in %s appended to %s", fname1,fname2);
      fclose(fp1);
      fclose(fp2);
      return 0;
}

C Program to print contents of file

#include<stdio.h>


int main()
{
    FILE *fptr;

    char filename[100], c;

    printf("Enter the filename to open \n");
    scanf("%s", filename);

    // Open file
    fptr = fopen(filename, "r");
    if (fptr == NULL)
    {
        printf("Cannot open file \n");
        exit(0);
    }

    // Read contents from file
    c = fgetc(fptr);
    while (c != EOF)
    {
        printf ("%c", c);
        c = fgetc(fptr);
    }

    fclose(fptr);
    return 0;
}

Morse Code:

Morse code is a method of transmitting text information as a series of on-off tones, lights, or clicks that can be directly understood by a skilled listener or observer without special equipment. It is named for Samuel F. B. Morse, an inventor of the telegraph.

The algorithm is very simple. Every character in the English language is substituted by a series of ‘dots’ and ‘dashes’ or sometimes just singular ‘dot’ or ‘dash’ and vice versa.

Every text string is converted into the series of dots and dashes. For this every character is converted into its Morse code and appended in encoded message. Here we have copied space as it is. And we have not considered numbers, only alphabets.

 

Input :    p    r  o  g   r    a   m 

Output : .--. .-. --- --. .-.  .-   --

 

Input :     p    p    s

Output : .--.  .--.  ...

 


Wednesday, December 4, 2019

Write a program in C to print individual characters of string in reverse order

#include<stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
    char str[100]; /* Declares a string of size 100 */
    int l=0;
   
  printf("\n\nPrint individual characters of string in reverse order :\n");
       printf("------------------------------------------------------\n");    
       printf("Input the string : ");
       fgets(str, sizeof str, stdin);
       l=strlen(str);
       printf("The characters of the string in reverse are : \n");
       for(str[l]='\0';l
>=0;l--)
        {
          printf("%c  ", str[l]);
        }
    printf("\n");
}
 

OUTPUT:
Print individual characters of string in reverse order :
------------------------------------------------------
Input the string : hello
The characters of the string in reverse are :
 
  o  l  l  e  h

Write a program in C to find transpose of a given matrix

#include<stdio.h>

int main()
{
    int mat[10][10];
    int i,j,r,c;
    
    printf("Enter number of Rows :");
    scanf("%d",&r);
    printf("Enter number of Cols :");
    scanf("%d",&c);

    printf("\nEnter matrix elements :\n");
    for(i=0;i
< r;i++)
    {
        for(j=0;j
< c;j++)
        {
            printf("Enter element [%d,%d] : ",i,j);
            scanf("%d",&mat[i][j]);
        }
    }

    /*Transpose a matrix */
    printf("\nTranspose Matrix is : \n");
    for(i=0;i
< c;i++)
    {
        for(j=0;j
< r;j++)
        {
            printf("%d\t",mat[j][i]);   
        }
        printf("\n");        
    }
    return 0;      
}
-------------------------------------------------------------
OUTPUT1:


Enter number of Rows :2
Enter number of Cols :2

Enter matrix elements :
Enter element [0,0] : 1
Enter element [0,1] : 2
Enter element [1,0] : 3
Enter element [1,1] : 4

Transpose Matrix is :
1    3   
2    4   

--------------------------------------------------------------
OUTPUT2:

Enter number of Rows :3
Enter number of Cols :3

Enter matrix elements :
Enter element [0,0] : 1
Enter element [0,1] : 2
Enter element [0,2] : 3
Enter element [1,0] : 4
Enter element [1,1] : 5
Enter element [1,2] : 6
Enter element [2,0] : 7
Enter element [2,1] : 8
Enter element [2,2] : 9

Transpose Matrix is :
1    4    7   
2    5    8   
3    6    9