Monday, February 28, 2011
./wc : : word count , determining the size of a file (in bytes, kilo bytes, in mega bytes) , no of characters in a file , no. of Lines in a File , No. of Words in a File , and arrangment in a descent order in the target file..... USING C in LINUX and UNIX
#include "stdio.h"
#include "string.h"
#include "malloc.h"
main(int argc, char **argv)
{
FILE *fp,*ft;
int count=0, i=0 , size=0 , word_count=0;
char *str,ch,c;
char *ptr;
if(argc!=3)
{
printf("ERROR: Invalid no. of Arguments\n");
printf("Usage: wc \n");
return;
}
fp=fopen(argv[1],"r");
/* Determining the Size of the File */
fseek(fp,SEEK_SET,SEEK_END);
size=ftell(fp);
printf("The size of the file is : %d bytes or %d KiloBytes or %d Megabytes\n",size,size/1024,size/(1024*1024));
fclose(fp);
size++;
str = calloc(1,size); // Allocate the size required to storethe Characters of the FILE into a String
fp=fopen(argv[1],"r");
/* Storing the Characters of the File into a Character Array or String */
for(i=0;i
str[i]=fgetc(fp);
fclose(fp);
/* Operation on The String */
printf("The Number of Characters = %d\n", strlen(str)); //The Number of Characters in the file
/* Number of Lines */
for(i=0;str[i];i++)
{
if(str[i]=='\n')
{
count++;
}
}
printf("No of lines: %d\n",count);
/* Remove Extra Blank Spaces (If any) and then Calculate the Number of Words */
/* Remove Blank Spaces */
ptr=str;
while(ptr=strchr(ptr,' '))
{
if(ptr[1]==' ')
{
strcpy(ptr+1,ptr+2);
ptr--;
}
ptr++;
}
/* Remove Consecutive New Lines */
ptr=str;
while(ptr=strchr(ptr,'\n'))
{
if(ptr[1]=='\n')
{
strcpy(ptr+1,ptr+2);
ptr--;
}
ptr++;
}
/* Removing Space from the starting of a line */
ptr=str;
while(ptr=strchr(ptr,'\n'))
{
if(ptr[1]==' ')
{
strcpy(ptr+1,ptr+2);
ptr--;
}
ptr++;
}
/* Removing consecutive NEW line */
ptr=str;
while(ptr=strchr(ptr,'\n'))
{
if(ptr[1]=='\n')
{
strcpy(ptr+1,ptr+2);
ptr--;
}
ptr++;
}
/* Count Number of Words */
for(i=0,count=0;str[i];i++)
{
if(str[i]=='\n')
{
count++;
}
}
for(i=0;str[i];i++)
{
if(str[i]==' ')
{
word_count++;
}
}
printf("No of words: %d\n",(word_count+count));
ft=fopen(argv[2],"w");
fputs(str,ft);
fclose(ft);
}
OUTPUT:
------------------------------------------ USAGE OF THE command----------------------------------
somanath@ubuntu:~/programs/files$ cc wordcount.c -o wc
somanath@ubuntu:~/programs/files$ ./wc demon filter
The size of the file is : 329 bytes or 0 KiloBytes or 0 Megabytes
The Number of Characters = 329
No of lines: 18
No of words: 27
------------------------------------- CONTENTS OF SOURCE FILE --------------------------------------
somanath@ubuntu:~/programs/files$ ./readfile demon
ravan is a demon
SOM is a hero
SOM killed ravan
Ravan got assacinated by Somanath Nanda in a game of gambling....... in splits VILAA
-------- PRASO
------------------------------------- CONTENTS OF TARGET FILE --------------------------------------
somanath@ubuntu:~/programs/files$ ./readfile filter
ravan is a demon
SOM is a hero
SOM killed ravan
Ravan got assacinated by Somanath Nanda in a game of gambling....... in splits VILAA
-------- PRASO
#include "string.h"
#include "malloc.h"
main(int argc, char **argv)
{
FILE *fp,*ft;
int count=0, i=0 , size=0 , word_count=0;
char *str,ch,c;
char *ptr;
if(argc!=3)
{
printf("ERROR: Invalid no. of Arguments\n");
printf("Usage: wc
return;
}
fp=fopen(argv[1],"r");
/* Determining the Size of the File */
fseek(fp,SEEK_SET,SEEK_END);
size=ftell(fp);
printf("The size of the file is : %d bytes or %d KiloBytes or %d Megabytes\n",size,size/1024,size/(1024*1024));
fclose(fp);
size++;
str = calloc(1,size); // Allocate the size required to storethe Characters of the FILE into a String
fp=fopen(argv[1],"r");
/* Storing the Characters of the File into a Character Array or String */
for(i=0;i
str[i]=fgetc(fp);
fclose(fp);
/* Operation on The String */
printf("The Number of Characters = %d\n", strlen(str)); //The Number of Characters in the file
/* Number of Lines */
for(i=0;str[i];i++)
{
if(str[i]=='\n')
{
count++;
}
}
printf("No of lines: %d\n",count);
/* Remove Extra Blank Spaces (If any) and then Calculate the Number of Words */
/* Remove Blank Spaces */
ptr=str;
while(ptr=strchr(ptr,' '))
{
if(ptr[1]==' ')
{
strcpy(ptr+1,ptr+2);
ptr--;
}
ptr++;
}
/* Remove Consecutive New Lines */
ptr=str;
while(ptr=strchr(ptr,'\n'))
{
if(ptr[1]=='\n')
{
strcpy(ptr+1,ptr+2);
ptr--;
}
ptr++;
}
/* Removing Space from the starting of a line */
ptr=str;
while(ptr=strchr(ptr,'\n'))
{
if(ptr[1]==' ')
{
strcpy(ptr+1,ptr+2);
ptr--;
}
ptr++;
}
/* Removing consecutive NEW line */
ptr=str;
while(ptr=strchr(ptr,'\n'))
{
if(ptr[1]=='\n')
{
strcpy(ptr+1,ptr+2);
ptr--;
}
ptr++;
}
/* Count Number of Words */
for(i=0,count=0;str[i];i++)
{
if(str[i]=='\n')
{
count++;
}
}
for(i=0;str[i];i++)
{
if(str[i]==' ')
{
word_count++;
}
}
printf("No of words: %d\n",(word_count+count));
ft=fopen(argv[2],"w");
fputs(str,ft);
fclose(ft);
}
OUTPUT:
Source File : demon
Target File : filter
------------------------------------------ USAGE OF THE command----------------------------------
somanath@ubuntu:~/programs/files$ cc wordcount.c -o wc
somanath@ubuntu:~/programs/files$ ./wc demon filter
The size of the file is : 329 bytes or 0 KiloBytes or 0 Megabytes
The Number of Characters = 329
No of lines: 18
No of words: 27
------------------------------------- CONTENTS OF SOURCE FILE --------------------------------------
somanath@ubuntu:~/programs/files$ ./readfile demon
ravan is a demon
SOM is a hero
SOM killed ravan
Ravan got assacinated by Somanath Nanda in a game of gambling....... in splits VILAA
-------- PRASO
------------------------------------- CONTENTS OF TARGET FILE --------------------------------------
somanath@ubuntu:~/programs/files$ ./readfile filter
ravan is a demon
SOM is a hero
SOM killed ravan
Ravan got assacinated by Somanath Nanda in a game of gambling....... in splits VILAA
-------- PRASO
All the features of C++ are Demonstrated in One Program
#include"iostream"
using namespace std;
class Height
{
private:
int feet, inches;
public:
/* Default Constructor */
Height()
{
feet=0;
inches=0;
}
/* Parametrized Constructor */
Height(int temp)
{
feet=temp;
inches=temp;
}
/*Parametrized Constructor */
Height(int a, int b)
{
feet=a;
inches=b;
}
/* Copy Constructor */
Height(const Height &ht)
{
feet=ht.feet;
inches=ht.inches;
}
/* Member Function to set Values */
void setValues()
{
cout<<"Enter dimension in feet";
cin>>feet;
cout<<"Enter dimension in inches";
cin>>inches;
}
/* Overloaded Member Function to set values e:g: if the dimension is 2 feet 5 inches*/
void setValues(int a, int b)
{
feet=a;
inches=b;
}
/* Overloaded Member Function to set values e:g: if the dimension is 5 feet 5 inches*/
void setValues(int a)
{
feet=a;
inches=a;
}
/* Member Function to print the Dimension */
void print()
{
cout<<<" feet "<<<" inches "<
}
/* '+' Operator Overloading */
Height operator + (Height x)
{
Height sum;
inches = (feet*12) + inches;
x.inches = (x.feet*12) + x.inches;
sum.feet = (inches + x.inches)/12;
sum.inches = (inches + x.inches)%12;
return sum;
}
/* '-' Operator Overloading */
Height operator - (Height x)
{
Height gap;
inches = (feet*12) + inches;
x.inches = (x.feet*12) + x.inches;
gap.feet = absolute((inches - x.inches))/12;
gap.inches = absolute((inches - x.inches))%12;
return gap;
}
/* function Which will operate as Modulo Operator */
int absolute (int num)
{
if(num<0)
return (-num);
else
return (num);
}
};
int main()
{
Height Ant;
cout<<"Height of Ant is:"<
Ant.print();
Height table(2,8);
cout<<"Height of Table is:"<
table.print();
Height ku;
ku = table + Ant;
cout<<"Height of ku is:"<
ku.print();
Height stick(2);
cout<<"Height of Stick is:"<
stick.print();
Height dog;
dog = table-stick;
cout<<"Height of dog is:"<
dog.print();
Height RosePlant;
RosePlant.setValues(1,4);
cout<<"Height of RosePlant is:"<
RosePlant.print();
Height HebiscusPlant;
HebiscusPlant = RosePlant + stick - Ant + dog;
cout<<"Height of HebiscusPlant is:"<
HebiscusPlant.print();
Height cosmos=RosePlant;
cout<<"Height of cosmos is:"<
cosmos.print();
return 0;
}
OUTPUT:
somanath@ubuntu:~/programs/CPP/28feb$ g++ Height.cpp
somanath@ubuntu:~/programs/CPP/28feb$ ./a.out
Height of Ant is:
0 feet 0 inches
Height of Table is:
2 feet 8 inches
Height of ku is:
2 feet 8 inches
Height of Stick is:
2 feet 2 inches
Height of dog is:
2 feet 6 inches
Height of RosePlant is:
1 feet 4 inches
Height of HebiscusPlant is:
6 feet 0 inches
Height of cosmos is:
1 feet 16 inches
using namespace std;
class Height
{
private:
int feet, inches;
public:
/* Default Constructor */
Height()
{
feet=0;
inches=0;
}
/* Parametrized Constructor */
Height(int temp)
{
feet=temp;
inches=temp;
}
/*Parametrized Constructor */
Height(int a, int b)
{
feet=a;
inches=b;
}
/* Copy Constructor */
Height(const Height &ht)
{
feet=ht.feet;
inches=ht.inches;
}
/* Member Function to set Values */
void setValues()
{
cout<<"Enter dimension in feet";
cin>>feet;
cout<<"Enter dimension in inches";
cin>>inches;
}
/* Overloaded Member Function to set values e:g: if the dimension is 2 feet 5 inches*/
void setValues(int a, int b)
{
feet=a;
inches=b;
}
/* Overloaded Member Function to set values e:g: if the dimension is 5 feet 5 inches*/
void setValues(int a)
{
feet=a;
inches=a;
}
/* Member Function to print the Dimension */
void print()
{
cout<
}
/* '+' Operator Overloading */
Height operator + (Height x)
{
Height sum;
inches = (feet*12) + inches;
x.inches = (x.feet*12) + x.inches;
sum.feet = (inches + x.inches)/12;
sum.inches = (inches + x.inches)%12;
return sum;
}
/* '-' Operator Overloading */
Height operator - (Height x)
{
Height gap;
inches = (feet*12) + inches;
x.inches = (x.feet*12) + x.inches;
gap.feet = absolute((inches - x.inches))/12;
gap.inches = absolute((inches - x.inches))%12;
return gap;
}
/* function Which will operate as Modulo Operator */
int absolute (int num)
{
if(num<0)
return (-num);
else
return (num);
}
};
int main()
{
Height Ant;
cout<<"Height of Ant is:"<
Ant.print();
Height table(2,8);
cout<<"Height of Table is:"<
table.print();
Height ku;
ku = table + Ant;
cout<<"Height of ku is:"<
ku.print();
Height stick(2);
cout<<"Height of Stick is:"<
stick.print();
Height dog;
dog = table-stick;
cout<<"Height of dog is:"<
dog.print();
Height RosePlant;
RosePlant.setValues(1,4);
cout<<"Height of RosePlant is:"<
RosePlant.print();
Height HebiscusPlant;
HebiscusPlant = RosePlant + stick - Ant + dog;
cout<<"Height of HebiscusPlant is:"<
HebiscusPlant.print();
Height cosmos=RosePlant;
cout<<"Height of cosmos is:"<
cosmos.print();
return 0;
}
OUTPUT:
somanath@ubuntu:~/programs/CPP/28feb$ g++ Height.cpp
somanath@ubuntu:~/programs/CPP/28feb$ ./a.out
Height of Ant is:
0 feet 0 inches
Height of Table is:
2 feet 8 inches
Height of ku is:
2 feet 8 inches
Height of Stick is:
2 feet 2 inches
Height of dog is:
2 feet 6 inches
Height of RosePlant is:
1 feet 4 inches
Height of HebiscusPlant is:
6 feet 0 inches
Height of cosmos is:
1 feet 16 inches
Sunday, February 27, 2011
Copy the contents of source file to TARGET file using command line argument in C
#include"stdio.h"
main(int argc, char **argv)
{
FILE *s_ptr , *t_ptr;
char ch,c;
if(argc!=3)
{
printf("ERROR: Invalid no. of Arguments\n");
printf("Usage: copy \n");
return;
}
s_ptr = fopen(argv[1],"r");
if(s_ptr==NULL)
{
printf("Source File not Found\n");
return;
}
t_ptr = fopen(argv[2],"r+");
if(t_ptr==NULL)
{
printf("Target File not Found\n");
printf("Do you want to create it (Y/N):\n");
scanf(" %c",&ch);
if(ch=='y' || ch=='Y')
{
t_ptr = fopen(argv[2],"w");
while((c=fgetc(s_ptr))!=EOF)
fputc(c,t_ptr);
}
else
return;
}
fclose(s_ptr);
fclose(t_ptr);
}
main(int argc, char **argv)
{
FILE *s_ptr , *t_ptr;
char ch,c;
if(argc!=3)
{
printf("ERROR: Invalid no. of Arguments\n");
printf("Usage: copy
return;
}
s_ptr = fopen(argv[1],"r");
if(s_ptr==NULL)
{
printf("Source File not Found\n");
return;
}
t_ptr = fopen(argv[2],"r+");
if(t_ptr==NULL)
{
printf("Target File not Found\n");
printf("Do you want to create it (Y/N):\n");
scanf(" %c",&ch);
if(ch=='y' || ch=='Y')
{
t_ptr = fopen(argv[2],"w");
while((c=fgetc(s_ptr))!=EOF)
fputc(c,t_ptr);
}
else
return;
}
fclose(s_ptr);
fclose(t_ptr);
}
Renaming a File Using command Line Argument in 'C'
#include
main(int argc, char **argv)
{
FILE *s_ptr , *t_ptr;
char ch,c;
if(argc!=3)
{
printf("ERROR: Invalid no. of Arguments\n");
printf("Usage: copy \n");
return;
}
s_ptr = fopen(argv[1],"r");
if(s_ptr==NULL)
{
printf("Source File not Found\n");
return;
}
t_ptr = fopen(argv[2],"r+");
if(t_ptr==NULL)
{
t_ptr = fopen(argv[2],"w");
while((c=fgetc(s_ptr))!=EOF)
fputc(c,t_ptr);
}
fclose(s_ptr);
fclose(t_ptr);\
remove(argv[1]);
}
main(int argc, char **argv)
{
FILE *s_ptr , *t_ptr;
char ch,c;
if(argc!=3)
{
printf("ERROR: Invalid no. of Arguments\n");
printf("Usage: copy
return;
}
s_ptr = fopen(argv[1],"r");
if(s_ptr==NULL)
{
printf("Source File not Found\n");
return;
}
t_ptr = fopen(argv[2],"r+");
if(t_ptr==NULL)
{
t_ptr = fopen(argv[2],"w");
while((c=fgetc(s_ptr))!=EOF)
fputc(c,t_ptr);
}
fclose(s_ptr);
fclose(t_ptr);\
remove(argv[1]);
}
Enter Date Month Year and get the Exact Day...
This is the C Program.................... Calender.c ................... Run it and check it out... If there is any fault, then in4m..... Comments are welcome....
#include
#include
main()
{
int date, month, year, days, nyear;
int x,y,k,weeks,new, remainder;
int odd_days, total_odd_days,leap_odd_days,leapyear_odd_days;
printf("Enter date-month-year");
scanf("%d%d%d",&date,&month,&year);
nyear=year-1;
x=nyear%100;
new=nyear-x;
remainder=new%400;
if(remainder%400==0)
leap_odd_days=0;
if(remainder%400==100)
leap_odd_days=5;
if(remainder%400==200)
leap_odd_days=3;
if(remainder%400==300)
leap_odd_days=1;
y=x/4;
k=(y+x)%7;
leapyear_odd_days=k+leap_odd_days;
if(month==1)
days=date;
else if(month==2)
days=31+date;
else if(month==3)
{
if(year%4==0)
days=31+29+date;
else
days=31+28+date;
}
else if(month==4)
{
if(year%4==0)
days=31+29+31+date;
else
days=31+28+31+date;
}
else if(month==5)
{
if(year%4==0)
days=31+29+31+30+date;
else
days=31+28+31+30+date;
}
else if(month==6)
{
if(year%4==0)
days=31+29+31+30+31+date;
else
days=31+28+31+30+31+date;
}
else if(month==7)
{
if(year%4==0)
days=31+29+31+30+31+30+date;
else
days=31+28+31+30+31+30+date;
}
else if(month==8)
{
if(year%4==0)
days=31+29+31+30+31+30+31+date;
else
days=31+28+31+30+31+30+31+date;
}
else if(month==9)
{
if(year%4==0)
days=31+29+31+30+31+30+31+31+date;
else
days=31+28+31+30+31+30+31+31+date;
}
else if(month==10)
{
if(year%4==0)
days=31+29+31+30+31+30+31+31+30+date;
else
days=31+28+31+30+31+30+31+31+30+date;
}
else if(month==11)
{
if(year%4==0)
days=31+29+31+30+31+30+31+31+30+31+date;
else
days=31+28+31+30+31+30+31+31+30+31+date;
}
else if(month==12)
{
if(year%4==0)
days=31+29+31+30+31+30+31+31+30+31+30+date;
else
days=31+28+31+30+31+30+31+31+30+31+30+date;
}
weeks=days/7;
odd_days=days%7;
total_odd_days=leapyear_odd_days + odd_days;
if((total_odd_days % 7 )== 0)
printf("\nSUNDAY");
if((total_odd_days % 7 )== 1)
printf("\nMONDAY");
if((total_odd_days % 7 )== 2)
printf("\nTUESDAY");
if((total_odd_days % 7 )== 3)
printf("\nWEDNESDAY");
if((total_odd_days % 7 )== 4)
printf("\nTHURSDAY");
if((total_odd_days % 7 )== 5)
printf("\nFRIDAY");
if((total_odd_days % 7 )== 6)
printf("\nSATURDAY");
printf("\n");
}
#include
#include
main()
{
int date, month, year, days, nyear;
int x,y,k,weeks,new, remainder;
int odd_days, total_odd_days,leap_odd_days,leapyear_odd_days;
printf("Enter date-month-year");
scanf("%d%d%d",&date,&month,&year);
nyear=year-1;
x=nyear%100;
new=nyear-x;
remainder=new%400;
if(remainder%400==0)
leap_odd_days=0;
if(remainder%400==100)
leap_odd_days=5;
if(remainder%400==200)
leap_odd_days=3;
if(remainder%400==300)
leap_odd_days=1;
y=x/4;
k=(y+x)%7;
leapyear_odd_days=k+leap_odd_days;
if(month==1)
days=date;
else if(month==2)
days=31+date;
else if(month==3)
{
if(year%4==0)
days=31+29+date;
else
days=31+28+date;
}
else if(month==4)
{
if(year%4==0)
days=31+29+31+date;
else
days=31+28+31+date;
}
else if(month==5)
{
if(year%4==0)
days=31+29+31+30+date;
else
days=31+28+31+30+date;
}
else if(month==6)
{
if(year%4==0)
days=31+29+31+30+31+date;
else
days=31+28+31+30+31+date;
}
else if(month==7)
{
if(year%4==0)
days=31+29+31+30+31+30+date;
else
days=31+28+31+30+31+30+date;
}
else if(month==8)
{
if(year%4==0)
days=31+29+31+30+31+30+31+date;
else
days=31+28+31+30+31+30+31+date;
}
else if(month==9)
{
if(year%4==0)
days=31+29+31+30+31+30+31+31+date;
else
days=31+28+31+30+31+30+31+31+date;
}
else if(month==10)
{
if(year%4==0)
days=31+29+31+30+31+30+31+31+30+date;
else
days=31+28+31+30+31+30+31+31+30+date;
}
else if(month==11)
{
if(year%4==0)
days=31+29+31+30+31+30+31+31+30+31+date;
else
days=31+28+31+30+31+30+31+31+30+31+date;
}
else if(month==12)
{
if(year%4==0)
days=31+29+31+30+31+30+31+31+30+31+30+date;
else
days=31+28+31+30+31+30+31+31+30+31+30+date;
}
weeks=days/7;
odd_days=days%7;
total_odd_days=leapyear_odd_days + odd_days;
if((total_odd_days % 7 )== 0)
printf("\nSUNDAY");
if((total_odd_days % 7 )== 1)
printf("\nMONDAY");
if((total_odd_days % 7 )== 2)
printf("\nTUESDAY");
if((total_odd_days % 7 )== 3)
printf("\nWEDNESDAY");
if((total_odd_days % 7 )== 4)
printf("\nTHURSDAY");
if((total_odd_days % 7 )== 5)
printf("\nFRIDAY");
if((total_odd_days % 7 )== 6)
printf("\nSATURDAY");
printf("\n");
}
555 Timer Pulse generation
The simplest 555 oscillator takes output pin 3 to capacitor C1 via resistor R1. When the circuit is turned on, C1 is uncharged and output pin 3 is HIGH. C1 charges via R1 and when Pin 6 detects 2/3 rail voltage, output pin 3 goes LOW. R1 now discharges capacitor C1 and when pin 2 detects 1/3 rail voltage, output pin 3 goes HIGH to repeat the cycle.
The amount of time when the output is HIGH is called the MARK and the time when the output is LOW is called the SPACE. In the diagram the mark is the same length as the space and this is called 1:1 or 50%:50%. If a resistor and capacitor (or electrolytic) is placed on the output, the result is very similar to a sinewave.
The amount of time when the output is HIGH is called the MARK and the time when the output is LOW is called the SPACE. In the diagram the mark is the same length as the space and this is called 1:1 or 50%:50%. If a resistor and capacitor (or electrolytic) is placed on the output, the result is very similar to a sinewave.
Subscribe to:
Posts (Atom)