4) Write a C program that reads in a string from the keyboard. Use scanf with the conversion code %s. Recall that the 2nd arg in scanf should be the address of the location in memory into which the inputted string should be stored. Recall that the name of an array without square brackets is the address of the first slot of the array. 5) Write a program that reads in this file (lab5.txt) and counts and displays the number of lowercase e's in the file. Use the fread function to read the entire file with just one call of fread. Use google to get info on fread.
#include <stdio.h> int main() { char str[100]; printf("Enter a string: "); scanf("%s", str); printf("You entered %s\n", str); return 0; }
#include <stdio.h> int main() { char str[1000]; int count = 0, i = 0; FILE *fp = fopen("lab5.txt", "r"); if (fp) { fread(str, sizeof(char), 1000, fp); while (str[i]) { if (str[i] == 'e') { ++count; } i++; } printf("Number of e's in file is %d\n", count); } else { fclose(fp); } return 0; }
Get Answers For Free
Most questions answered within 1 hours.