Simple GUI Notepad Using Ruby

GUI Notepad Using Ruby Code require 'tk' class Notepad def saveFile file = File.open("note", "w") ...

Thursday, January 19, 2017

Read A Text File Using C

Read a text file using C

Open a text file using C program and read the content of the file.


Code

/*Write a program to read a file*/
#include <stdio.h>
#include <stdlib.h>

int main()
{
 FILE *fp; //Taking a file pointer variable
 char fname[30],ch;

 //Taking a existing file name from the user to read
 printf("\nEnter a file name to read: ");
 gets(fname);

 //Open the file in read mode
 fp=fopen(fname,"r");
 
 //Check the file is corrupted or not
 if(fp==NULL)
 {
  printf("\nFile cannot be access.");
  exit(0);
 }
 if(ferror(fp)!=0)
 {
  printf("\nFile is either corrupted or damaged");
  exit(1);
 }

 //Print the content of the file
 while((ch=getc(fp))!=EOF)
 {
  printf("%c",ch);
 }

 //Close the file
 fclose(fp);

 return 0;
}

Output>

Enter a file name to read: temp.txt
This is a temporary file.
Note:
To read a file you have to create a file first by using any text editor(Notepad) or you can also use the C program to create a file.

C Program to Store Record In A File

C program to store students records in a file

The main purpose of this program is that to store some information in a file from console.

Code

/*Write a program that will read roll, name and marks of several student from the user and store them in a file*/
#include <stdio.h>
#include <stdlib.h>
//Create a structure for store the records for all students
struct student
{
 int roll;
 char name[30];
 int marks;
} s;
int main( ){
 FILE *fp; //Create a file pointer variable
 char fname[20],s2[20];
 int n,i,s1,s3;
 printf("\nEnter a file name to store student record: ");
 gets(fname);
 //Open the file in write mode
 fp=fopen(fname,"w");
 if(fp==NULL){
  printf("\nError:FIle cannot be created. \a");
  exit(0);
 }
 printf("\nEnter the number of student: ");
 scanf("%d",&n);
 //Taking the records of students and store them in the file
 for(i=0;i<n;i++){
  printf("\nFor student no. %d", i+1);
  printf("\nEnter roll: ");
  scanf("%d",&s.roll);
  printf("\nEnter name: ");
  scanf("%s",&s.name);
  printf("\nEnter marks: ");
  scanf("%d",&s.marks);
  fprintf(fp,"%d\t%s\t%d\n",s.roll,s.name,s.marks);
 }
 //Close the file
 fclose(fp);
 //Open the file in read mode
 fp=fopen(fname,"r");
 if(fp==NULL){
  printf("\nError:FIle does not exist. \a");
  exit(0);
 }
 if(ferror(fp)!=0){
  printf("\nError: File is either corrupted or damaged. \a");
  exit(0);
 }

 //Print the content of the file
 for(i=1;i<=n;i++){
  fscanf(fp,"%d\t%s\t%d\n",&s1,&s2,&s3);
  printf("\n%d\t%s\t%d",s1,s2,s3);
 }
 //Close the file
 fclose(fp);
 return(0);
}

Output

Enter a file name to store student record: student.txt
Enter the number of student: 3
For student no. 1
Enter roll: 101
Enter name: Alex
Enter marks: 85
For student no. 2
Enter roll: 102
Enter name: Rose
Enter marks: 96
For student no. 3
Enter roll: 103
Enter name: Charls
Enter marks: 93
The content of the file is:
101     Alex    85
102     Rose    96
103     Charls  93

Replace A Word In A File

Replace a word in a file

Find a specific word from an existing file and replace that word with another.

Code

/******Word replace in a file******/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
 FILE *fp,*fp1;
 char str[15],str1[15],fname[20],ch;
 int i=0,len,flag,x=0;
 //Creating a file
 printf("\nEnter a file name:");
 gets(fname);
 fp=fopen(fname,"w");
 if(fp==NULL){
  printf("\nFile cannot be created.");
  exit(0);
 }
 if(ferror(fp)!=0){
  printf("\nFile become damage or corrupt.");
  exit(0);
 }
 //Enter the content in the file
 printf("\nEnter the content of the file. press ctrl+z after compulsion.\n\n");
 while((ch=getchar())!=EOF)
  putc(ch,fp);
 fclose(fp);
 //Open the file in read mode
 fp=fopen(fname,"r");
 if(fp==NULL){
  printf("\nFile cannot be created.");
  exit(0);
 }
 if(ferror(fp)!=0){
  printf("\nFile become damage or corrupt.");
  exit(0);
 }
 //Print the content of the file
 printf("\nThe content of the current file is:\n");
 while((ch=getc(fp))!=EOF)
  printf("%c",ch);
 fclose(fp);
 //Open the file in read mode
 fp=fopen(fname,"r");
 if(fp==NULL){
  printf("\nFile cannot be created.");
  exit(0);
 }
 if(ferror(fp)!=0){
  printf("\nFile become damage or corrupt.");
  exit(0);
 }
 //Taking input of the word to be replace
 printf("\nEnter the word you want to replace: ");
 scanf("%s",str);
 len=strlen(str);
 printf("\nEnter replaced word: ");
 scanf("%s",str1);
 //Open a new file to copy all the content of the old file with the replace word
 fp1=fopen("new.txt","w");
 if(fp==NULL){
  printf("\nFile cannot be created.");
  exit(0);
 }
 if(ferror(fp)!=0){
  printf("\nFile become damage or corrupt.");
  exit(0);
 }
 while((ch=getc(fp))!=EOF){
  if((ch!=32) && (ch!='\n')){
   flag=0; 
   i=0;
   do{
    if((ch==str[i]) && (i<len))
     i++;
    else{
     flag=1;
     break;
    }   
   }while(((ch=getc(fp))!=EOF) && (ch!=32) && (ch!='.') && (ch!='\n') && (ch!='?') && (ch!='!'));
  
   if(flag==0){
    fprintf(fp1,"%s",str1);
    if(ch==32) putc(ch,fp1);
    if(ch=='\n') putc(ch,fp1);
    if(ch=='?') fprintf(fp1,"%c ",ch);
    if(ch=='!') fprintf(fp1,"%c ",ch);
    if(ch=='.') fprintf(fp1,"%c ",ch);
    x++;
   }
   else{
    if(i!=0)
     fseek(fp,-(i+1),SEEK_CUR);
    do{
     putc(ch,fp1);
    }while(((ch=getc(fp))!=EOF) && (ch!=32) && (ch!='.') && (ch!='\n') && (ch!='?') && (ch!='!'));
    if(ch==32) putc(ch,fp1);
    if(ch=='\n') putc(ch,fp1);
    if(ch=='?') fprintf(fp1,"%c ",ch);
    if(ch=='!') fprintf(fp1,"%c ",ch);
    if(ch=='.') fprintf(fp1,"%c ",ch);
   }
  }
 }
 printf("\n%d word(s) are found and replace successfully.",x);
 fclose(fp);
 fclose(fp1);

 fp1=fopen("new.txt","r");
 printf("\nThe content of the new file is:\n");
 while((ch=getc(fp1))!=EOF)
  printf("%c",ch);
 
 fclose(fp1);
 return 0;
}

Output


Enter a file name:temp.txt
Enter the content of the file. press ctrl+z after compulsion.
This is a temp file. This file is for testing.
^Z
The content of the current file is:
This is a temp file. This file is for testing.
Enter the word you want to replace: file
Enter replaced word: program
2 word(s) are found and replace successfully.
The content of the new file is:
This is a temp program. This program is for testing.

C Program to Count From A File

C program to count from a file

Count the numbers of characters, vowels, consonant, lines, words, punctuation marks from a existing file.


Code

/*Write a program that will read the contains of a file and count the characters, vowels, consonant, lines, words, punctuation marks */
#include <stdio.h>
#include <stdlib.h>

int main()
{
 FILE *fp;
 char fname[30],ch;
 int c=0,v=0,ln=0,w=0,p=0;

 //Taking a file name from the user
 printf("\nEnter a file name to read: ");
 gets(fname);

 //Open the file in read mode
 fp=fopen(fname,"r");
 
 //Check is the file exist or not
 if(fp==NULL)
 {
  printf("\nFile cannot be access.");
  exit(0);
 }

 //Print the content of the file
 printf("\nThe contain of the file is: \n");
 while((ch=getc(fp))!=EOF)
 {
  printf("%c",ch);
  
  //Checking and count the outputs
  if((ch>=65&&ch<=90)||(ch>=97&&ch<=122))
  {
   c=c+1;
  }
  if((ch=='a')||(ch=='e')||(ch=='i')||(ch=='o')||(ch=='u')||(ch=='A')||(ch=='E')||(ch=='I')||(ch=='O')||(ch=='U'))
  {
   v=v+1;
  }
  if((ch=='.')||(ch=='?')||(ch=='!'))
  {
   ln=ln+1;
   w=w+1;
  }
  if(ch==' ')
  {
   w=w+1;
  }
  if((ch==';')||(ch=='"')||(ch==','))
  {
   p=p+1;
  }
 }
 //Print the outputs
 printf("\nNumber of character in the file is %d",c);  //Count the characters
 printf("\nNumbers of vowels in the file is %d",v);               //Count the vowels
 printf("\nNumbers of consonant in the file is %d",(c-v));       //Count the consonants
 printf("\nNumbers of lines in the file is %d",ln);              //Count the lines
 printf("\nNumbers of words in the file is %d",w);               //Count the words
 printf("\nNumbers of punctuation marks in the file is %d",p);   //Count the punctuation marks
 fclose(fp);

 return 0;
}

Output

Enter a file name to read: temp.txt

The containt of the file is:
This is a temporary file.

Number of charecter in the file is 20
Numbers of vouls in the file is 8
Numbers of consonent in the file is 12
Numbers of lines in the file is 1
Numbers of words in the file is 5
Numbers of punchuation marks in the file is 0

Monday, January 16, 2017

Create File In C

Create file in C

Create a text file in C and enter some content in it and close the file.


Code

/****Write a program to create a file****/
#include <stdio.h>
#include <stdlib.h>

int main()
{

 FILE *fp; //Taking a file pointer variable
 char fname[30],ch;

 //Taking a file name from the user with its extension
 printf("\nEnter a file name: ");
 gets(fname);

 //Open the file in write mode
 fp=fopen(fname,"w");
 if(fp==NULL)
 {
  printf("\nFile cannot be created.");
  exit(0);
 }

 //Write the content from the console to the file
 printf("\nEnter contain of the file: \n");
 while((ch=getchar())!=EOF)
 {
  putc(ch,fp);
 }

 printf("\nFile created successfully.");
 //Close the file
 fclose(fp);

 return 0;
}

Output

Enter a file name: temp.txt

Enter contain of the file:
This is a temporary file.
^Z

File created successfully.

Sunday, October 2, 2016

Find Minimum In Stack In O(1) Time and O(1) Space

Find Minimum In Stack In O(1) Time and O(1) Space

For this problem I have used a stack of pointer to nodes. A node have two members one is the value part and another is the link to another node.

Conditions

  • Update the minimum with each push operation
  • A node (current) will point to another node if the current node have a previous minimum node else it will point to null
  • During pop check the top node and the minimum node if they point to the same location then update the current minimum to previous minimum by assigning the link of the top node to minimum pointer
  • If the top node has null to its link part then it is not the minimum node and no pointer manipulation needed,

Sample Operations

C++ Implementation

#include <iostream>
using namespace std;
#define MAX 9999

struct Node {
    int val;
    struct Node *add;
};

class Stack
{
    Node **arr, *minn;
    int s, top;

    public:
        Stack(int s) : s(s) {
            arr = new Node*[s];
            top = -1;
            minn = new Node();
            minn->val = MAX;
        }
        ~Stack() {
            delete [] arr;
        }

        void push(int item);
        int pop();
        int minimum() const {
            return (top ==-1)? -1 : minn->val;
        };
};

void Stack::push(int item) {
    if (top == s-1) {
        cout << "Stack full!" << endl;
    }
    else {
        Node *tmp = new Node();
        tmp->add = NULL;
        if (top == -1) {
            minn = tmp;
        }
        else {
            if (item <= minn->val) {
                tmp->add = minn;
                minn = tmp;
            }
        }
        tmp->val = item; ++top;
        arr[top] = tmp;
    }
}

int Stack::pop()
{
    int retVal = -1;
    if (top == -1) {
        delete minn;
        cout << "Stack empty!" << endl;
    }
    else {
        retVal = arr[top]->val;
        if (arr[top] == minn){
            minn = arr[top]->add;
        }
        top--;
    }
    return retVal;
}

int main()
{
    Stack s(6);
    s.push(7);
    s.push(1);
    s.push(1);
    s.push(2);
    s.push(4);
    s.push(0);
    cout << "Current Min: " << s.minimum() << endl;
    cout << "Popped: " << s.pop() << endl;
    cout << "Current Min: " << s.minimum() << endl;
    cout << "Popped: " << s.pop() << endl;
    cout << "Current Min: " << s.minimum() << endl;
    cout << "Popped: " << s.pop() << endl;
    cout << "Current Min: " << s.minimum() << endl;
    cout << "Popped: " << s.pop() << endl;
    cout << "Current Min: " << s.minimum() << endl;
    cout << "Popped: " << s.pop() << endl;
    cout << "Current Min: " << s.minimum() << endl;
    cout << "Popped: " << s.pop() << endl;
    return 0;
}
Current Min: 0
Popped: 0
Current Min: 1
Popped: 4
Current Min: 1
Popped: 2
Current Min: 1
Popped: 1
Current Min: 1
Popped: 1
Current Min: 7
Popped: 7

Process returned 0 (0x0)   execution time : 0.044 s
Press any key to continue.
If you find anything wrong please comment below. And if you have any other approach then mail me at itsaboutcs@gmail.com.

Wednesday, August 3, 2016

Java Facts

Just In Time (JIT) Compilation:

Java is designed as a interpreted language. Java code is converted into byte code and that is interpreted by JVM. As we know interpreted languages are slows compared to compiled language because compiled code is ore closer to machine architecture. But java seems to have no major issue with its performance because of the
  • Highly optimized byte code, and
  • JIT compilation
Some selected portion of java byte code is converted into machine code on the fly.

Final Method Increases Performance:

In Java we can define a method as final to avoid overriding. And when we do so there is a chance of performance increase. If the final method is short then java will not call the method instead it will copy the byte code of that particular routine at the place of its call. This technique is called inline calls in Java similar to inline function in C++ where we can explicitly define an inline function (although the final decision is made by compiler). For a final method call can be resolved in compiler time called early binding.