My intention is to create a loop where the tokens phrases
are compared. If the phrase is unique (not the same as any others) then it is printed and if the phrase is not unique then tally
increases by one and the repeat phrase is not printed. So each phrase is printed only once and later I'll add code to show how many times each phrase was counted.
The program compiles without errors and when it runs there is no output, the program ends and exits. The functions filter_spaces
and upper_case
are working fine, and it did print the phrases before I changed for (token = strtok(string, strip); token; token = strtok(NULL, strip))
to the for
loops.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void filter_spaces(char* string);
void upper_case(char string[]);
int main(){
char c, string[201];
int i = 0, k=0, j=0;
char strip[] = ",.:;?!";
char* phrases[1000];
int tally = 0;
printf("What gibberish shall I parse?\n");
while((c = getchar()) != EOF) //while loop for getting characters from stdin
string[i++] = c;
string[i] = '\0'; //insert NULL at end of string
filter_spaces(string);
upper_case(string);
char *phrase = strtok(string, strip);
puts("\nThe phrases entered are: ");
/*for (token = strtok(string, strip); token; token = strtok(NULL, strip))
{
puts(token);
}
*/
while(phrase != '\0')
{
phrases[i++] = strdup(phrase);
phrase = strtok(NULL, strip);
for(k=0; k<i-1; k++)
{
for(j = k+1; j<k; j++)
{
if(strcmp(phrases[k], phrases[j])==0)
{
tally = tally++; //add to phrases[k] count
}
else
{
printf("<%s>\n", phrase); //print and continue on
}
}
}
}
return 0;
}
void upper_case(char string[])
{
int c = 0;
while (string[c] != '\0')
{
if (string[c] >= 'a' && string[c] <= 'z')
{
string[c] = string[c] - 32;
}
c++;
}
}
void filter_spaces(char* string)
{
int i,x;
for(i=x=1; string[i]; ++i)
if(string[i]!=' ' || string[i+1]!=' ')
string[x++] = string[i];
string[x] = '\0';
}
Hope it would helps you. Your logic to count repeated and print unique character was wrong.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void filter_spaces(char* string);
void upper_case(char string[]);
int main()
{
char c, string[201];
int i = 0, k = 0, j = 0;
char strip[] = " ,.:;?!";
char* phrases[1000];
int tally = 0;
printf("What gibberish shall I parse?\n");
while((c = getchar()) != '\n') //while loop for getting characters from stdin
string[i++] = c;
string[i] = '\0'; //insert NULL at end of string
filter_spaces(string);
upper_case(string);
char *phrase = strtok(string, strip);
puts("\nThe phrases entered are: ");
/*
for (token = strtok(string, strip); token; token = strtok(NULL, strip))
{
puts(token);
}
*/
i=0;
while(phrase != '\0')
{
phrases[i] = strdup(phrase);
int flag=0;
for(k=0; k<i; k++)
{
if(strcmp(phrases[k], phrase)==0)
{
tally = tally++; //add to phrases[k] count
flag=1;
break;
}
}
if(flag==0)
{
printf("**** %s \n", phrase); //print and continue on
}
phrase = strtok(NULL, strip);
++i;
}
printf("Total Number of repeated Words = %d\n",tally);
return 0;
}
void upper_case(char string[])
{
int c = 0;
while (string[c] != '\0')
{
if (string[c] >= 'a' && string[c] <= 'z')
{
string[c] = string[c] - 32;
}
c++;
}
}
void filter_spaces(char* string)
{
int i,x;
for(i=x=1; string[i]; ++i)
if(string[i]!=' ' || string[i+1]!=' ')
string[x++] = string[i];
string[x] = '\0';
}