Count all signs, not white signs, all words and lines from char array in C

Viewed 93

I get a task. I must write a program consists of several functions to analyze text (count words, signs, lines, not white signs etc.) This is my program:

#include <stdio.h>
#include <stdlib.h>

int allSigns(char text[])
{

    int i;
    for (i = 0; text[i] != '\0'; ++i);    
    return i;
}

int notWhiteSigns(char text[])
{
    int sum;
    for (int i = 0; text[i] != '\0'; ++i)
    {
        if (text[i] != (' ' && '\n' && '\t'))
            sum++;
    }
    return sum;
}

int words(char text[])
{
    int i;
    int sum = 0;
    for (int i = 1; text[i] != '\0'; ++i)
    {
        if (text[i] == ' ' && text[i-1] && text[i] != ('\t' && '\n'))
            sum++;
    }
    if (text[i-1] != ' ');
        sum++;
    return sum;
}

int lines(char text[])
{
    int i;
    int sum = 1;
    for (int i = 0; text[i] != '\0'; ++i)
    {
        if (text[i] == '\n')
            sum++;
    }
    return sum;
}


int main (int argc, char *argv[])
{

    char sentence[] = "C is \n a \n programming \t language";

    printf("Show array: %s \n", sentence);

    printf("All signs: %i\n not white signs: %i\n words: %i\n lines: %i\n", allSigns(sentence), notWhiteSigns(sentence), words(sentence), lines(sentence));


    return 0;
}

My output is:

Show array: C is 
 a 
 programming     language 
All signs: 33
 not white signs: 66
 words: 8
 lines: 3

I see that I made some mistakes. Lines is ok (3). But I have 8 words instead of 5 words. Function that count all words is ok (I count \n and \t as one). I dont know why I have 66 "not white signs" (all signs except ' ', '\n', '\t',) I should have 23

4 Answers

The error is here:

if (text[i] != (' ' && '\n' && '\t'))

Each character in (' ' && '\n' && '\t') will be represented as ASCII code. So this expression means (text[i] != (32 && 10 && 9)).

Try this instead:

if ((text[i] != ' ') && (text[i] != '\n') && (text[i] != '\t'))

You also should initialize sum variable before use.

You need to initialize sum with zero and change the if statement:

int notWhiteSigns(char text[])
    {
        int sum = 0;
        for (int i = 0; text[i] != '\0'; ++i)
        {
            if (text[i] != ' ' && text[i] != '\n' && text[i] != '\t')
                sum++;
        }
        return sum;
    }

in your code you have used two uninitialized variable:

first sum in function int notWhiteSigns(char text[]) which need to be int sum=0.

second here:

int words(char text[])
{
    int i;
    int sum = 0;
    for (int i = 1; text[i] != '\0'; ++i)
    {
        if ((text[i] != ' ') && (text[i] != '\n') && (text[i] != '\t'))
            sum++;
    }
    if (text[i - 1] != ' ');
    sum++;
    return sum;
}

since you have re declared i in for-loop you won't have it's value outside of loop , and so here if (text[i - 1] != ' '); you are using uninitialized i remove int i from for loop for (i = 1; text[i] != '\0'; ++i)

also as mentioned it's wrong to compare Text i with different values at once.

this should be if ((text[i] != ' ') && (text[i] != '\n') && (text[i] != '\t'))

Thank you function notwhiteSpace works correct. I found solution for counting words function

int words(char text[])
{
    int i;
    int sum = 0;
    for (int i = 1; text[i] != '\0'; ++i)
    {
        if (text[i] == ' ' && text[i-1] != ' ' && (text[i] != '\n') && (text[i] != '\t') && (text[i-1] != '\n') && (text[i-1] != '\t'))
            sum++;
    }
    if (text[i-1] != ' ');
        sum++;
    return sum;
}

Now it works properly

Related