#include <iostream> #include <fstream> using namespace std; int main() { ifstrea
ID: 3748607 • Letter: #
Question
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
string word[10];
char space = 32;
char letters[100];
int counter = 1;
int counter2 = 1;
infile.open("op.txt");
while(!infile){
cout << "file not working. " << endl;
break;
}
while(infile){
for(int i = 0; i < 27; i++){
infile >> letters[i];
cout << letters[i] << ": number of Lines, spaces, letters " << counter << endl;
counter++;
}
for(int i = 0; i < 27; i++){
if(letters[i] >= space || letters[i] <= 'z'){
letters[i];
if(letters[i] == space){
counter2++;
}
else{
cout << "The number of words: " << counter2 << endl;
}
}
}
}
infile.close();
return 0;
}
please convert to c
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *infile;
char ch;
char space = 32;
char letters[100];
int counter = 1;
int counter2 = 1;
infile = fopen("op.txt", "r"); // read mode
if(!infile){
printf("file not working. ");
return 1;
}
while((ch = fgetc(infile)) != EOF){
for(int i = 0; i < 27; i++){
letters[i] = fgetc(infile);
printf("%c number of Lines, spaces, letters %d ", letters[i],counter);
counter++;
}
for(int i = 0; i < 27; i++){
if(letters[i] >= space || letters[i] <= 'z'){
letters[i];
if(letters[i] == space){
counter2++;
}
else{
printf("The number of words: %d ",counter2);
}
}
}
}
fclose(infile);
return 0;
}
=============================================
Converted to C
Note: I did not check the logic. I just converted it. Logic you need to correct. I just converted to C syntax thats it
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.