Good evening! You need to write a program that counts the number of spaces, full stops and commas in the file. I tried to do this by creating an array of type char, but the teacher did not approve)) I do not quite understand how you can accomplish this task without creating an array, but using ifstream / ofstream and related functions. Is it possible to somehow save my program by making minor changes so that it works correctly?)) And I don’t really want to rewrite everything all over again .. ((Thanks in advance!
#include <iostream> #include <string.h> #include <cstdlib> #include <stdio.h> using namespace std; int main () { char a[1000]; cout << "Enter your text: " << endl; gets (a); int spc = 0, dt = 0, km = 0; int lng = strlen(a); for (int d = 0; d < lng; d++) { if (a[d] == ',') km++; } for (int c = 0; c < lng; c++) { if (a[c] == '.') dt++; } for (int b = 0; b < lng; b++) { if (a[b] == ' ') spc++; } if (spc >= 1) { cout << "The number of spaces in the text is: " << endl << spc << endl; } else { cout << "There are no spaces in the text" << endl; } if (dt >= 1) { cout << "The number of dots in the text is: " << endl << dt << endl; } else { cout << "There are no dots in the text" << endl; } if (km >= 1) { cout << "The number of commas in the text is: " << endl << km << endl; } else { cout << "There are no commas in the text" << endl; } }
strsep()your friend. - 0andriy