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