我正在嘗試撰寫一個 C 程式來計算單詞和空格的數量并檢查常用單詞。
這就是我所擁有的:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int stringCount(char *str) {
int count = 0, i, spaces;
for (i = 0; *str != '\0'; i ) {
if (*str == ' ') {
spaces ;
}
}
return count;
}
bool checkWord(char *str, char *word) {
int i = 0;
int j = 0;
while (str[i] != '\0') {
if (str[i] == word[j]) {
j ;
if (word[j] == '\0') {
return true;
}
} else {
i = i - j;
j = 0;
}
i ;
}
return false;
}
int main() {
char str1[20];
char str2[20];
char word[20];
int i = 0;
int j = 0;
int k = 0;
int count = 0;
int count2 = 0;
printf("Enter a string: ");
fgets(str1, 20, stdin);
printf("Enter a string: ");
fgets(str2, 20, stdin);
count = stringCount(str1);
count2 = stringCount(str2);
printf("The number of characters in string 1 is %d\n", count);
printf("The number of characters in string 2 is %d\n", count2);
printf("The common word is: ");
if (word[k] == '\0') {
printf("%c", word[k]);
k ;
} else {
printf("There is no Common Word");
}
printf("\n");
return 0;
}
編輯:我們禁止使用#include <string.h>
程式以某種方式運行
編輯:我被困在stringCount()了checkWord(). 因為stringCount需要用空格計算字串,但我不知道如何。并且checkWord()需要檢查是否有常用詞而不是字符。
uj5u.com熱心網友回復:
您的代碼中有多個問題:
- 讀取字串的陣列大小在 20 時似乎有點短。
- 您不檢查
fgets()可能未能讀取字串。如果從空檔案重定向,程式將具有未定義的行為。 fgets()如果有的話,將尾隨換行符存盤在陣列中。您應該在計數之前將其洗掉。您可以使用函式來讀取不會存盤換行符并忽略用戶輸入的額外字符的字串。stringCount()有一個無限回圈:您不會str在回圈中更新。你應該改為 teststr[i] != '\0'。- 您計算空格但不將其回傳給呼叫者。將指標傳遞給區域變數是從函式呼叫中獲取多個值的慣用方式。
checkWord不做這項作業:找到最長常用詞的簡單方法是蠻力:對于 中的每個詞str1,嘗試比較 中的每個詞str2。
這是修改后的版本:
#include <stdio.h>
/* read a string from the user */
int getString(const char *prompt, char *buf, int size) {
int c, i = 0;
printf("%s", prompt);
/* read a full line of characters, stop at newline or EOF */
while ((c = getchar()) != EOF && c != '\n') {
if (i 1 < size) {
buf[i ] = (char)c;
}
}
putchar('\n');
buf[i] = '\0';
if (c == EOF && i == 0) {
return -1;
} else {
return i;
}
}
/* count the characters and spaces in the string */
int stringCount(const char *str, int *sp) {
int i, spaces = 0;
for (i = 0; str[i] != '\0'; i ) {
if (str[i] == ' ')
spaces ;
}
if (sp != NULL)
*sp = spaces;
return i;
}
int findCommonWord(const char *str1, const char *str2, char *buf) {
int i, j, k, n1, n2;
int best_i = 0, best_len = 0;
for (i = 0; str1[i] != '\0'; i = n1) {
n1 = 1;
if (str1[i] == ' ')
continue;
/* compute length of word starting at str1[i] */
while (str1[i n1] != '\0' && str1[i n1] != ' ') {
n1 ;
}
/* if the word is longer than the best match, try and find it in str2 */
if (n1 > best_len) {
for (j = 0; str2[j] != '\0'; j = n2) {
n2 = 1;
if (str2[j] == ' ')
continue;
/* compute length of word starting at str2[j] */
while (str2[j n2] != '\0' && str2[j n2] != ' ') {
n2 ;
}
if (n1 == n2) {
for (k = 0; k < n1; k ) {
if (str1[i k] != str2[j k])
break;
}
/* if comparison succeeds we have a new best match */
if (k == n1) {
best_len = n1;
best_i = i;
}
}
}
}
}
/* copy the longest match */
for (k = 0; k < best_len; k ) {
buf[k] = str1[best_i k];
}
buf[k] = '\0';
return k;
}
int main() {
char str1[100], str2[100], word[100];
int count1, spaces1, count2, spaces2;
if (getString("Enter a string: ", str1, sizeof str1) < 0
|| getString("Enter a string: ", str2, sizeof str2) < 0)
return 1;
count1 = stringCount(str1, &spaces1);
count2 = stringCount(str2, &spaces2);
printf("string 1: %d characters, %d spaces\n", count1, spaces1);
printf("string 2: %d characters, %d spaces\n", count2, spaces2);
if (findCommonWord(str1, str2, word)) {
printf("The longest common word is: %s\n", word);
} else {
printf("There is no common word\n");
}
return 0;
}
輸出:
Enter a string: Hello world I am Benny
Enter a string: I am the world
string 1: 22 characters, 4 spaces
string 2: 14 characters, 3 spaces
The longest common word is: world
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/456849.html
標籤:C
上一篇:檢查是否設定了N(大)位
下一篇:我想從C語言的檔案中獲取雙精度值
