Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Escape character for tab

+2 votes
asked Nov 26, 2018 by OSHGroup (210 points)

I am using '\t' to try and match tabs on the input, but it doesn't seem to work.

Any thoughts?

#include <stdio.h>

int main()
{
    double n_tabs, n_newlines, n_blanks;
    
    int nextChar = getchar();
    
    while (nextChar != EOF)
    {
        if (nextChar == '\t') //Does not appear to work.
        {
            ++n_tabs; 
        }
        else if (nextChar == '\n')
        {
            ++n_newlines;
        }
        else if (nextChar == ' ')
        {
            ++n_blanks;
        }
        
        nextChar = getchar();
    }
    
    printf("# Tabs %d\n", n_tabs);
    printf("# Newlines %d\n", n_newlines);
    printf("# Blanks %d", n_blanks);

    return 0;
}


2 Answers

+1 vote
answered Nov 30, 2018 by anonymous
selected Dec 1, 2018 by OSHGroup
 
Best answer

#include <stdio.h>

// minor changes to make it work

int main()
{
    int n_tabs, n_newlines, n_blanks;
    
    n_tabs = n_newlines = n_blanks = 0;
    
    char nextChar = getchar();
    
    while (nextChar != EOF)
    {
        if (nextChar == '\t')
        {
            ++n_tabs;
        }
        else if (nextChar == '\n')
        {
            ++n_newlines;
        }
        else if (nextChar == ' ')
        {
            ++n_blanks;
        }
        
        nextChar = getchar();
    }
    
    printf("# Tabs %d\n", n_tabs);
    printf("# Newlines %d\n", n_newlines);
    printf("# Blanks %d", n_blanks);

    return 0;
}

+1 vote
answered Nov 27, 2018 by anonymous
you can compare the ascii value on both sides
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...