0% found this document useful (0 votes)
8 views6 pages

C Prog Week 10

Uploaded by

ssdharshini24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

C Prog Week 10

Uploaded by

ssdharshini24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

join FG Community: https://chat.whatsapp.

com/HWWkzXOgYld0esBGSYmFRT

Admin/ Gang Leader: Kavish

Assignments by: The Hairline

C Prog Week 10:

1. D
2. B, D
3. -10
4. C
5. B
6. D
7. A
8. A
9. 6
10. 12

GRPA 1:

void writeToFile() {

char inputString[51];

fgets(inputString, sizeof(inputString), stdin);

FILE *file = fopen("notes.txt", "w");

if (file == NULL) {

printf("Error opening file!\n");

exit(1);}

fprintf(file, "%s", inputString);

fclose(file);}
GRPA 2:

int countString() {

char s[20];

int flag = 0;

fgets(s, 20, stdin);

FILE *file = fopen("notes.txt", "r");

if (file == NULL) {

return -1;}

char bu er[256];

int count = 0;

while (fgets(bu er, sizeof(bu er), file) != NULL) {

char *pos = bu er;

while ((pos = strstr(pos, s)) != NULL) {

count++;

pos += strlen(s);}}

fclose(file);

return count;}
GRPA 3:

void readFile()

FILE *file;

file = fopen("output.txt", "r");

int number;

if (file != NULL)

while (fscanf(file, "%d", &number) != EOF)

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

else

printf("File not found");

fclose(file);

}
GRPA 4:

int count_words(FILE* fp){

if (fp==NULL){

return 0;}

int count = 0;

char c, prev_c;

c=prev_c =' ';

while ((c= fgetc(fp))!=EOF){

if (

prev_c != ' ' &&

prev_c != '\n' &&

(c == ' ' || c =='\n')){

count++;}

prev_c = c;}

if (prev_c!=' ' && prev_c!='\n'){count++;}

return count;}
GRPA 5:

#include <stdlib.h>

void findToppers() {

FILE *fp = fopen("score.csv", "r");

if (fp == NULL) {

printf("Error: Cannot open file.\n");

return;

char line[100];

char name[51];

int score;

int highestScore = -1;

char toppers[100][51];

int topperCount = 0;

while (fgets(line, sizeof(line), fp)) {

sscanf(line, "%50[^,],%d", name, &score);

if (score > highestScore) {

highestScore = score;

topperCount = 0;

strcpy(toppers[topperCount++], name);

} else if (score == highestScore) {

strcpy(toppers[topperCount++], name);

}
fclose(fp);

for (int i = 0; i < topperCount; i++) {

printf("%s\n", toppers[i]);

You might also like