Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctype.h>
- #include <stdio.h>
- //Automaton states
- enum Automata {
- Start,
- Short_com,
- Long_com,
- String
- };
- int main(int agrc, char *argv[]) {
- enum Automata cur_state;
- cur_state = Start;
- char symbol;
- int counter = 0;
- while (EOF != (symbol = getchar())) {
- //Check if we can go in some not Start state
- if (symbol == '/' && cur_state == Start) {
- char next_symbol = getchar();
- if (next_symbol == '/') {
- cur_state = Short_com;
- }
- else if (next_symbol == '*') {
- cur_state = Long_com;
- }
- else { //because of EOF
- break;
- }
- continue; //No need to count this symbols
- }
- if (symbol == '"' && cur_state == Start) {
- cur_state = String;
- ++counter;
- continue; //Already count symbol
- }
- //Trying to go to the Starting state
- if (cur_state == Short_com && symbol == '\n') {
- cur_state = Start;
- continue; //No need to count this symbol
- }
- if (cur_state == Long_com && symbol == '*') {
- char next_symbol = getchar();
- if (next_symbol == '/') {
- cur_state = Start;
- }
- continue; //No need to count this symbols
- }
- if (cur_state == String && symbol == '"') {
- cur_state = Start;
- ++counter;
- continue; //Already count symbol
- }
- //Counting if in String or Start
- if (cur_state == Start || cur_state == String) {
- if (!isspace(symbol)) {
- ++counter;
- }
- }
- //printf("%d", cur_state);
- }
- printf("%d", counter);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement