我的目的是创建一个比较令牌phrases
的循环。如果短语是唯一的(与其他短语不同)则打印,如果短语不唯一,则tally
增加1,不打印重复短语。因此,每个短语只打印一次,之后我会添加代码以显示每个短语的计数次数。
程序编译时没有错误,当它运行时没有输出,程序结束并退出。函数filter_spaces
和upper_case
工作正常,在我将for (token = strtok(string, strip); token; token = strtok(NULL, strip))
更改为for
循环之前,它确实打印了这些短语。
#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';
}
答案 0 :(得分:0)
您的程序不起作用,因为您没有正确关闭while循环。 试试这个:
#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';
}
答案 1 :(得分:0)
希望它会对你有所帮助。你计算重复和打印独特字符的逻辑是错误的。
#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';
}