You are on page 1of 10

Sheet09

strings, Fileoperations, Commandolinearguments, Struktures

1. write a program that reads a string str_in of the keyboard and creates a
second string str_out of twice the length. All letters in "str_in" should be
doubled in "str_out" and "str_out" is to be displayed on the screen. Example:
str_in="String" , str_out="SSttrriinngg"

#include <stdio.h>

int main()

{

char str_in[100], str_out[100], i, j;

printf("Enter string str_in: ");

scanf("%s",str_in);

for(i = 0, j = 0; str_in[i] != '\0'; ++i,++j)

{

str_out[j] = str_in[i];

++j;

str_out[j] = str_in[i];

}

str_out[j] = '\0';

printf("String str_out: %s", str_out);

return 0;

}
2. write a program that reads the first 10 words of a file into an array and then
displys the words line by line on the screen.

3. Write a program called "add" to add numbers that are entered in the command
line and output them on the screen.

Example: call add 1 2 3 4 generates the output "10".

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

void main(int argc,char *argv[])
{
int sum=0,i;
//Compare if proper number of arguments have been entered
if(argc<3)
{
printf("Insufficient number of arguments:\n");
getch();
return 0;
}

//Add all the numbers entered using atoi function
for(i=1;i<argc;i++)
{
sum+=atoi(argv[i]);
}

//print the sum
printf("Ans=%d",sum);
getch();
}

4. Write a program that calculates the power of two natural numbers a, b in the
command line (sufficiently small) and places this result in a file whose name is also
specified in the command line.
Example: The call power 2 3 MyOut.txt saves the result "8" into a file named
"MyOut.txt"

5. Define a structure "student"with the components "name", "first name", "course
of studies", "student number" and "subject semester". Consider suitable data types
for the individual structural components. The component "name" should be a fixed-
length char array n. What is the value of n when storing names with a maximum of
20 characters?

6. Define a structure for managing account data with the components "account
holder", "account number" and "bank sort code". Consider suitable data types for
the individual structural components. Write a main program, which reads the data
for a structural element from the keyboard.

7. Write a program mycopy that copies a file. The name of the file name to be
copied is to be passed as a command line line parameter.

Example: mycopy Original.txt Copy.txt

#include <stdio.h>

#include <conio.h>

#include <process.h>

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

{

FILE *fs,*ft;

char ch;

clrscr();

if(argc!=3) //here it must pass 3 arguments thru argv namely mycopy,
source file and destination file

{

puts("Invalid number of arguments.");

exit(0);

}
fs = fopen(argv[1],"r"); //argv[1] Source file name -> READ MODE

if(fs==NULL)

{

puts("Source file cannot be opened.");

exit(0);

}

ft = fopen(argv[2],"w"); //argv[2] Destination file name -> WRITE MODE

if (ft==NULL)

{

puts("Target file cannot be opened.");

fclose(fs);

exit(0);

}

while(1) // WHILE TRUE COPY THE CONTENTS FROM SOURCE TO
DESTINATION

{

ch=fgetc(fs);

if (ch==EOF)

break;

else

fputc(ch,ft);

}

fclose(fs);

fclose(ft);

getch();

}
8. Write a function "stringcompare" that has two strings as input parameters,
compares them, and returns "1" as the result, if both are identical , otherwise
returns "0".

#include<stdio.h>

int stringCompare(char[],char[]);

int main(){

char str1[100],str2[100];

int compare;

printf("Enter first string: ");

scanf("%s",str1);

printf("Enter second string: ");

scanf("%s",str2);

compare = stringCompare(str1,str2);

if(compare == 1)

printf("Both strings are equal.");

else

printf("Both strings are not equal");

return 0;

}
int stringCompare(char str1[],char str2[]){

int i=0,flag=0;

while(str1[i]!='\0' && str2[i]!='\0'){

if(str1[i]!=str2[i]){

flag=1;

break;

}

i++;

}

if (flag==0 && str1[i]=='\0' && str2[i]=='\0')

return 1; // Returns 1 when both are equal

else

return 0; // Returns 0 when both are not equal

}

9. Write a function "filecompare" that has two file names as input parameters,
compares both files, and returns "1" as the result,if both are identical, and otherwise
returns "0"

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

Int filecompare(void);
void main()
{
int result = filecompare();
printf("tn %d",result);

If (result == 1)
{

printf("Files are identical n"); }

else

{

printf("Files are Not identical n");

}
}

int filecompare(void)

{

FILE *fp1, *fp2;

int ch1, ch2;

char fname1[40], fname2[40];

printf("Enter name of first file :"); //GET FILE 1 NAME FROM USER

gets(fname1);

printf("Enter name of second file:"); //GET FILE 2 NAME FROM USER

gets(fname2);

// OPEN BOTH THE FILES IN READ MODE

fp1 = fopen(fname1, "r");

fp2 = fopen(fname2, "r");

//CHECK IF BOTH THE FILES ARE ACCESSIBLE

if (fp1 == NULL) {

printf("Cannot open %s for reading ", fname1);

exit(1);

} else if (fp2 == NULL) {
printf("Cannot open %s for reading ", fname2);

exit(1);

} else {

ch1 = getc(fp1); //GET 1ST CHAR FROM FILE ONE IN CH1

ch2 = getc(fp2); //GET 1ST CHAR FROM FILE TWO IN CH2

//TRAVERSE BOTH THE FILES AND MOVE ON ONLY IF BOTH THE CHARACTERS ARE
EQUAL

//ELSE BREAK THE WHILE LOOP

while ((ch1 != EOF) && (ch2 != EOF) && (ch1 == ch2)) {

ch1 = getc(fp1);

ch2 = getc(fp2);

}

//THE END OF WHILE LOOP IS REACHED -> NOW CHECK THE LAST CHARACTERS
AND PRINT RESULT IF //BOTH ARE EQUAL ELSE PRINT BOTH FILES ARE NOT EQUAL

if (ch1 == ch2)

printf("Files are identical n");

return 1; //RETURN 1 IF BOTH FILES ARE EQUAL

else if (ch1 != ch2)

printf("Files are Not identical n");

return 0; //RETURN 0 IF BOTH FILES ARE NOT EQUAL

fclose(fp1);

fclose(fp2);

}

}