Let Us C / Chapter 13 (More Issues in Input Output)

                                   Exercise [B]


(a) Write a program to carry out the following:
(a) Read a text file provided at command prompt
(b) Print each word in reverse order
For example if the file contains
INDIA IS MY COUNTRY
Output should be
AIDNI SI YM YRTNUOC

Solution:


#include"stdio.h"<+>
#include<conio.h>
#include<string.h>

void main(int argc, char *argv[]) {


FILE *fs;
char ch,s[80];
void rev();

if(argc!=2) {

printf("File name not supplied\n");

exit();

}


fs=fopen(argv[1],"r");

if(fs==NULL) {

printf("File not found!");

exit();
}

while(fgets(s,79,fs)!=NULL)
rev(s);

fclose(fs);

}

void rev(char s1[80]) {

char s2[80];
int i=0,j=0;

while(s1[i]!='\0') {

s2[j]=s1[i];

if(s1[i]==' ' || s1[i]=='\0') {

s2[j]='\0';

strrev(s2);

printf("%s ",s2);

j=-1;

}

i++;
j++;
}

s2[j]='\0';

printf("%s",strrev(s2));


}

-----------------------------------------------------------------------------------------------------------------

(b) Write a program using command line arguments to search for a word in a file and replace it with the specified word. The usage of the program is shown below.
C> change <old word> <new word> <filename>

Solution:


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

void replace();

void main ( int argc, char *argv[] ) {

FILE *fp,*ft;
char str[80],str1[30],str2[30],nwstr[100];

clrscr();

if(argc!=4) {

puts("Improper argements passed!");
exit();
}

strcpy(str1,argv[1]);
strcpy(str2,argv[2]);

fp=fopen(argv[3],"r");

if(fp==NULL) {

puts("cannot open source file!");
exit();
}

ft=fopen("NEW.TXT","w+");

if(ft==NULL) {

puts("cannot open target file!");
exit();
}

while(fgets(str,79,fp)!=NULL) {

replace(str,str1,str2,&nwstr);

fputs(nwstr,ft);
}

fclose(fp);
fclose(ft);

remove("FILE.TXT");
rename("NEW.TXT","FILE.TXT");

printf("\n\nTask completed!\n\n");

getch();

}

void replace(char *s, char s1[80], char s2[80], char *n) {

int i=0,j=0,k=0;
char temp[100],temp2[100],main[100],*t=temp,*m=main;


/*******************************/
/* copying to temporary string */
/*******************************/


while(*s!='\0') {

*t=*s;

t++;
s++;

}

*t='\0';

/**********************/
/* checking each word */
/**********************/


while(temp[i]!='\0') {

temp2[j]=temp[i];

if(temp[i]==' ' || temp[i]==',' || temp[i]=='.') {

temp2[j]='\0';

if(strcmpi(temp2,s1)==0) {

strcpy(temp2,s2);

}

j=0;

while(temp2[j]!='\0') {

main[k]=temp2[j];

k++;
j++;
}

main[k]=' ';  /* adding space after each word is copied */

k++;     /* increment so that the next word won't replace the space */

j=-1;

}

i++;
j++;
}

temp2[j]='\0';              /* last word terminated */

if(strcmpi(temp2,s1)==0){    /* checking last word too */

strcpy(temp2,s2);

}

/***************************/
/* last word of the string */
/***************************/

j=0;

while(temp2[j]!='\0') {

main[k]=temp2[j];

k++;
j++;
}

main[k]='\0';   /* new string is completely ready */


/******************************************************************/
/* preparing new string to return to main and printing it on file */
/******************************************************************/

while(*m!='\0') {

*n=*m;

n++;
m++;
}

*n='\0';


}

----------------------------------------------------------------------------------------------------------------

(c) Write a program that can be used at command prompt as a calculating utility. The usage of the program is shown below.
C> calc <switch> <n> <m>
Where, n and m are two integer operands. switch can be any one of the arithmetic or comparison operators. If arithmetic operator is supplied, the output should be the result of the operation. If comparison operator is supplied then the output should be True or False.

Solution:


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

void main(int argc, char *argv[]) {

int n,m,calc;

clrscr();

if(argc!=4)
printf("Improper number of arguments");


/* converting both operands to integers */


n=atoi(argv[2]);

m=atoi(argv[3]);

printf("\n\nOutput:  ");


/* comparing the operators passed at command prompt */



if(strcmp("+",argv[1])==0) {

printf("%d\n\n",n+m);

}

if(strcmp("-",argv[1])==0) {

printf("%d\n\n",n-m);

}

if(strcmp("*",argv[1])==0) {

printf("%d\n\n",n*m);

}

if(strcmp("/",argv[1])==0) {

printf("%d\n\n",n/m);

}

if(strcmp("%",argv[1])==0) {

printf("%d\n\n",n%m);

}
  
  /* IMPORTANT */
  

/*   I have used dedicated variable for '<' and '>' comparison operators   */
/*   because at command prompt, these are redirecting operators and cannot */
/*   be used simply for comparison.                                        */

  
 

    /* 1:  "<" = "less"       */
    /* 2:  ">" = "greater" */
   
/* type "less" for "<" and "greater" for ">" respectively if you want to perform comparison */
    

if(strcmp("greater",argv[1])==0) {

if(n>m)
printf("True\n\n");

if(n<m)
printf("False\n\n");

}

if(strcmp("less",argv[1])==0) {

if(n<m)
printf("True\n\n");

if(n>m)
printf("False\n\n");

}

if(strcmp("=",argv[1])==0) {

if(n==m)
printf("True\n\n");

else
printf("False\n\n");

}

}



_______________________________________________________________________

No comments:

Post a Comment

Let Us C / Chapter 7 (The C Pre-processor)

                               Exercise [C] (a) Write down macro definitions for the following: 1. To test whether a character ente...