为什么我会得到如此混乱的输出?
Why do I get such a messed up output?
我正在编写一个程序来打印出现的字母和(一个、两个、三个字母等)单词。到目前为止,我已经让字母部分正常工作,但我根本无法让单词部分正常工作,更不用说区分一个、两个或三个字母的单词了。我试图找出程序出错的地方,似乎是在我尝试将它们存储在数组 "word".
中的时候
有人建议使用 strlok(),但没有提及如何使用。
编辑:我用 sizeof() 替换了 strlen() 并设置了我的 'i' 变量 = 0 它应该在哪里,但我的输出仍然是整个字符串的第一个字母和一些奇怪的字符.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void findLetters(char *ptr);
void findWords(char *point);
int main()
{
char textStream[100]; //up to 98 characters and '\n\ and '[=10=]'
printf ( "enter some text\n");
if ( fgets( textStream, sizeof ( textStream), stdin)) //input up to 99 characters
{
findLetters(textStream);
}
else
{
printf ( "fgets failed\n");
}
findWords(textStream);
return 0;
}
void findLetters(char *ptr) //find occurences of all letters
{
int upLetters[26];
int loLetters[26];
int i;
int index;
for ( i = 0; i < 26; i++) // set array to all zero
{
upLetters[i] = 0;
loLetters[i] = 0;
}
i = 0;
while ( ptr[i] != '[=10=]') // loop until prt[i] is '[=10=]'
{
if (ptr[i] >= 'A' && ptr[i] <= 'Z') //stores occurrences of uppercase letters
{
index = ptr[i] - 'A';// subtract 'A' to get index 0-25
upLetters[index]++;//add one
}
if (ptr[i] >= 'a' && ptr[i] <= 'z') //stores occurrences of lowercase letters
{
index = ptr[i] - 'a';//subtract 'a' to get index 0-25
loLetters[index]++;//add one
}
i++;//next character in ptr
}
printf("Number of Occurrences of Uppercase letters\n\n");
for ( i = 0; i < 26; i++)//loop through 0 to 25
{
if ( upLetters[i] > 0)
{
printf("%c : \t%d\n", (char)(i + 'A'), upLetters[i]);
// add 'A' to go from an index back to a character
}
}
printf("\n");
printf("Number of Occurrences of Lowercase letters\n\n");
for ( i = 0; i < 26; i++)
{
if ( loLetters[i] > 0)
{
printf("%c : \t%d\n", (char)(i + 'a'), loLetters[i]);
// add 'a' to go back from an index to a character
}
}
}
void findWords(char *point)
{
int i = 0;
int k = 0;
int count = 0;
int j = 0;
int space = 0;
int c = 0;
char word[50][100], word1[50][100];
for (;i < sizeof(point);i++) //counts # of spaces between words
{
if ((point[i] == ' ')||(point[i] == ',')||(point[i] == '.'))
{
space++;
}
}
i = 0;
for(; i < sizeof(point); i++) //seperates strings from each other
{
if(point[i] == '.' || point[i] == 44|| point[i] == 46)
{
word[j][k] = '[=10=]';
j++;
k = 0;
printf("%s\n",point[i]);
}
else
{
word[j][k] = point[i];
k++;
}
printf("%s\n",word[j]);
}
k = 0;
for (i = 0;i <= space;i++)
{
for (j = 0;j <= space;j++)
{
if (i == j) // finds occurrences of words
{
strcpy(word1[k], word[i]); //copies words in new array
k++;
count++;
}
else if(strcmp(word1[j], word[i]) != 0) //makes sure that the word copied equals the word from the string
{
;
}
}
}
j = 0;
i = 0;
for (;i < count ;i++)
{
for (;j <= space;j++)
{
if (strcmp(word1[i], word[j]) == 0) //counts occurrence of each word
{
c++;
}
}
printf("%s \t %d times\n", word1[i], c);
c = 0;
}
}
你的代码有几个问题:
for(;i < strlen(point); i++) //seperates strings from each other
当这一行执行时,i
的值已经是strlen
,因此后面的for
不会被执行。添加 i=0
来修复它。
你应该使用sizeof
而不是strlen,否则你错过了最后一个字。
if(point[i] == '.' || point[i] == 44|| point[i] == 46)
您没有在此处检查 ' '
。正确的 if 条件应该是:
if(point[i] == ' ' || point[i] == '.'|| point[i] == ' ')
您的代码没有考虑逗号和 space 在一起的情况。
您用于在数组中查找单词的算法存在缺陷。
你应该使用这样的东西:
how_many_times(word[i], word, number_of_words);
int how_many_times(char * word, char words[50][100], int how_many_words) {
int i = 0, counter=0;
for (i=0; i< how_many_words; i++) {
if ( strcmp(words[i], word) == 0 ) {
counter++;
}
}
return counter;
}
一个主要问题是你的第二个循环没有运行:
int i=0;
for (;i < strlen(point);i++) //counts # of spaces between words
{
...
}
for(;i < strlen(point); i++) //seperates strings from each other
{
...
}
i
在第二个 for
循环开始时仍然是 strlen(point)-1
,所以 运行 不是。我建议始终在 for 循环中指定起点,并且只计算一次字符串长度,而不是每个循环。
int len = strlen(point);
for (i=0; i < len; i++)
我正在编写一个程序来打印出现的字母和(一个、两个、三个字母等)单词。到目前为止,我已经让字母部分正常工作,但我根本无法让单词部分正常工作,更不用说区分一个、两个或三个字母的单词了。我试图找出程序出错的地方,似乎是在我尝试将它们存储在数组 "word".
中的时候有人建议使用 strlok(),但没有提及如何使用。
编辑:我用 sizeof() 替换了 strlen() 并设置了我的 'i' 变量 = 0 它应该在哪里,但我的输出仍然是整个字符串的第一个字母和一些奇怪的字符.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void findLetters(char *ptr);
void findWords(char *point);
int main()
{
char textStream[100]; //up to 98 characters and '\n\ and '[=10=]'
printf ( "enter some text\n");
if ( fgets( textStream, sizeof ( textStream), stdin)) //input up to 99 characters
{
findLetters(textStream);
}
else
{
printf ( "fgets failed\n");
}
findWords(textStream);
return 0;
}
void findLetters(char *ptr) //find occurences of all letters
{
int upLetters[26];
int loLetters[26];
int i;
int index;
for ( i = 0; i < 26; i++) // set array to all zero
{
upLetters[i] = 0;
loLetters[i] = 0;
}
i = 0;
while ( ptr[i] != '[=10=]') // loop until prt[i] is '[=10=]'
{
if (ptr[i] >= 'A' && ptr[i] <= 'Z') //stores occurrences of uppercase letters
{
index = ptr[i] - 'A';// subtract 'A' to get index 0-25
upLetters[index]++;//add one
}
if (ptr[i] >= 'a' && ptr[i] <= 'z') //stores occurrences of lowercase letters
{
index = ptr[i] - 'a';//subtract 'a' to get index 0-25
loLetters[index]++;//add one
}
i++;//next character in ptr
}
printf("Number of Occurrences of Uppercase letters\n\n");
for ( i = 0; i < 26; i++)//loop through 0 to 25
{
if ( upLetters[i] > 0)
{
printf("%c : \t%d\n", (char)(i + 'A'), upLetters[i]);
// add 'A' to go from an index back to a character
}
}
printf("\n");
printf("Number of Occurrences of Lowercase letters\n\n");
for ( i = 0; i < 26; i++)
{
if ( loLetters[i] > 0)
{
printf("%c : \t%d\n", (char)(i + 'a'), loLetters[i]);
// add 'a' to go back from an index to a character
}
}
}
void findWords(char *point)
{
int i = 0;
int k = 0;
int count = 0;
int j = 0;
int space = 0;
int c = 0;
char word[50][100], word1[50][100];
for (;i < sizeof(point);i++) //counts # of spaces between words
{
if ((point[i] == ' ')||(point[i] == ',')||(point[i] == '.'))
{
space++;
}
}
i = 0;
for(; i < sizeof(point); i++) //seperates strings from each other
{
if(point[i] == '.' || point[i] == 44|| point[i] == 46)
{
word[j][k] = '[=10=]';
j++;
k = 0;
printf("%s\n",point[i]);
}
else
{
word[j][k] = point[i];
k++;
}
printf("%s\n",word[j]);
}
k = 0;
for (i = 0;i <= space;i++)
{
for (j = 0;j <= space;j++)
{
if (i == j) // finds occurrences of words
{
strcpy(word1[k], word[i]); //copies words in new array
k++;
count++;
}
else if(strcmp(word1[j], word[i]) != 0) //makes sure that the word copied equals the word from the string
{
;
}
}
}
j = 0;
i = 0;
for (;i < count ;i++)
{
for (;j <= space;j++)
{
if (strcmp(word1[i], word[j]) == 0) //counts occurrence of each word
{
c++;
}
}
printf("%s \t %d times\n", word1[i], c);
c = 0;
}
}
你的代码有几个问题:
for(;i < strlen(point); i++) //seperates strings from each other
当这一行执行时,i
的值已经是strlen
,因此后面的for
不会被执行。添加i=0
来修复它。你应该使用
sizeof
而不是strlen,否则你错过了最后一个字。if(point[i] == '.' || point[i] == 44|| point[i] == 46)
您没有在此处检查' '
。正确的 if 条件应该是:if(point[i] == ' ' || point[i] == '.'|| point[i] == ' ')
您的代码没有考虑逗号和 space 在一起的情况。
您用于在数组中查找单词的算法存在缺陷。 你应该使用这样的东西:
how_many_times(word[i], word, number_of_words);
int how_many_times(char * word, char words[50][100], int how_many_words) {
int i = 0, counter=0;
for (i=0; i< how_many_words; i++) {
if ( strcmp(words[i], word) == 0 ) {
counter++;
}
}
return counter;
}
一个主要问题是你的第二个循环没有运行:
int i=0;
for (;i < strlen(point);i++) //counts # of spaces between words
{
...
}
for(;i < strlen(point); i++) //seperates strings from each other
{
...
}
i
在第二个 for
循环开始时仍然是 strlen(point)-1
,所以 运行 不是。我建议始终在 for 循环中指定起点,并且只计算一次字符串长度,而不是每个循环。
int len = strlen(point);
for (i=0; i < len; i++)