我的程序设计为允许用户输入字符串,我的程序将输出每个字母和单词的出现次数。我的程序也按字母顺序排列单词。在字符串数组中排序字
我的问题是:我输出单词看到(第一unsorted)和他们的事件作为表,并在我的表中,我不想重复。 求助
例如,如果单词“to”被看到两次,我只希望单词“to”在我的表中只出现一次,输出出现次数。
我该如何解决这个问题?另外,为什么我不能简单地将string[i] == delim
设置为适用于每个分隔符,而不必为每个分隔符手动分配?
编辑:修正了我的输出错误。但我怎样才能设置条件string[i]
等于我的代码中的任何分隔符,而不是仅仅为空格键工作?例如,在我的输出中,如果我输入“你,你”,它会输出“你,你”而不仅仅是“你”。我怎么写它,以便它消除逗号并将“你,你”比作一个单词。
任何帮助表示赞赏。我的代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const char delim[] = ", . - !*()&^%$#@<> ? []{}\\/\"";
#define SIZE 1000
void occurrences(char s[], int count[]);
void lower(char s[]);
int main()
{
char string[SIZE], words[SIZE][SIZE], temp[SIZE];
int i = 0, j = 0, k = 0, n = 0, count;
int c = 0, cnt[26] = { 0 };
printf("Enter your input string:");
fgets(string, 256, stdin);
string[strlen(string) - 1] = '\0';
lower(string);
occurrences(string, cnt);
printf("Number of occurrences of each letter in the text: \n");
for (c = 0; c < 26; c++){
if (cnt[c] != 0){
printf("%c \t %d\n", c + 'a', cnt[c]);
}
}
/*extracting each and every string and copying to a different place */
while (string[i] != '\0')
{
if (string[i] == ' ')
{
words[j][k] = '\0';
k = 0;
j++;
}
else
{
words[j][k++] = string[i];
}
i++;
}
words[j][k] = '\0';
n = j;
printf("Unsorted Frequency:\n");
for (i = 0; i < n; i++)
{
strcpy(temp, words[i]);
for (j = i + 1; j <= n; j++)
{
if (strcmp(words[i], words[j]) == 0)
{
for (a = j; a <= n; a++)
strcpy(words[a], words[a + 1]);
n--;
}
} //inner for
}
i = 0;
/* find the frequency of each word */
while (i <= n) {
count = 1;
if (i != n) {
for (j = i + 1; j <= n; j++) {
if (strcmp(words[i], words[j]) == 0) {
count++;
}
}
}
/* count - indicates the frequecy of word[i] */
printf("%s\t%d\n", words[i], count);
/* skipping to the next word to process */
i = i + count;
}
printf("ALphabetical Order:\n");
for (i = 0; i < n; i++)
{
strcpy(temp, words[i]);
for (j = i + 1; j <= n; j++)
{
if (strcmp(words[i], words[j]) > 0)
{
strcpy(temp, words[j]);
strcpy(words[j], words[i]);
strcpy(words[i], temp);
}
}
}
i = 0;
while (i <= n) {
count = 1;
if (i != n) {
for (j = i + 1; j <= n; j++) {
if (strcmp(words[i], words[j]) == 0) {
count++;
}
}
}
printf("%s\n", words[i]);
i = i + count;
}
return 0;
}
void occurrences(char s[], int count[]){
int i = 0;
while (s[i] != '\0'){
if (s[i] >= 'a' && s[i] <= 'z')
count[s[i] - 'a']++;
i++;
}
}
void lower(char s[]){
int i = 0;
while (s[i] != '\0'){
if (s[i] >= 'A' && s[i] <= 'Z'){
s[i] = (s[i] - 'A') + 'a';
}
i++;
}
}
您似乎误解了一些语法,或者犯了一个简单的错误:)'words [i] == NULL;'不做任何声明。你将'words'中的'i'与'NULL'进行比较,'=='是相等运算符。该声明甚至可能被编译器优化掉。它在语法上有效,但什么都不做,因为你不会对结果做任何事情。没有看过代码的其余部分,但你很可能希望'='(赋值) – Skurmedel
lower()被破坏:用_s [i] =(s [s]替换_s [i] = s [i] + 32_我] - 'A')+'a'_ –
谢谢我修复了Craig。 – Benny