the push to vaccinate children has taken on fresh urgency amid concerns that the new omicron variant of the virus first identified in southern africa and hong kong in late november will spread quickly in the united states causing a surge in infections already back on the rise from the easily transmitted delta variant given the pervasiveness of delta and prospects of new variants spreading in the united states having as much immunity in the population as possible is critical said dr amesh adalja senior scholar at the johns hopkins center for health security
這是我的任務:
- 將單詞之間的多個空格替換為一個空格,并洗掉開頭和結尾不需要的空格。
- 數字
- 列印編輯過的字串
- 不要使用新字串,只需編輯。
我找不到問題。它應該算的話,但它做不到。請幫幫我。
//Counting words program C
#include <stdio.h>
#define N 5000
int main(void) {
FILE *fp;
char text[N];
int k, d, leng, spacecount = 0;
int m, j, z, i, p, n;
if ((fp = fopen("soru.txt", "r")) == NULL) {
printf("Dosya acma hatasi!");
return 1;
}
fgets(text, N - 1, fp);
while (k < N && text[k] != '\0') {
leng ;
k ;
}
z = leng;
for (i = 0; i < leng; i ) {
if (i = 0 && text[i] == ' ') {
z--;
for (m = 0; m < leng; m ) {
text[m] = text[m 1];
}
i--;
text[z] == '\0';
} else
if (text[i] ==' ' && text[i 1] == ' ') {
z--;
for (j = i; j < leng; j ) {
text[j 1] = text[j 2];
}
i--;
text[z] == '\0';
} else
if (text[i] == ' ' && text[i 1] == '\0') {
z--;
for (j = i; j < leng; j ) {
text[j] = text[j 1];
}
i--;
text[z] == '\0';
} else
if (text[i] == '\0') {
break;
}
}
while (text[d] != '\0') {
if (text[d] == ' ')
spacecount ;
d ;
}
printf("kelime sayisi: %d" , spacecount 1);
printf("\n cikti:%s ", text);
fclose(fp);
return 0;
}
我找不到問題。它應該算這個詞,但它做不到。請幫幫我
for(i=0; i < leng; i ) {
if(i=0 && text[i]== ' '){
z--;
for(m=0; m< leng; m ){
text[m] = text [m 1];}
i--;
}
else if(1<i<z && text[i] ==' ' && text[i 1] == ' ' ){
z--;
for(j=i; j<leng ; j ) {
text[j 1] = text [j 2];}
i--;
}
else if(i=z && text[i] ==' ' && text[i 1] == '\0' ){
z--;
for(j=i; j<leng ; j ) {
text[j] = text [j 1]; }
i--;
}
},// I think problem in here. Endless loop
uj5u.com熱心網友回復:
你的代碼太復雜了。你可以用 2 個索引變數解決這個問題:一個從輸入行讀取字符,一個將相關字符寫入同一個緩沖區。
您將跟蹤前一個字符,從空格開始,并檢測單詞的開頭,因為當前字符不是空格后面的空格。因此,您將計算單詞并僅在每個單詞之前輸出一個空格,除了一行中的第一個。
這是一個修改后的版本:
//Counting words program C
#include <stdio.h>
#define N 5000
int main(void) {
FILE *fp;
char text[N];
int total_words = 0;
if ((fp = fopen("soru.txt", "r")) == NULL) {
printf("Dosya a?ma hatas?!\n");
return 1;
}
while (fgets(text, N, fp) != NULL) {
int len = strlen(text);
int word_count = 0;
char c, lastc = ' ';
int i, j;
// strip the trailing newline
if (len > 0 && text[len - 1] == '\n') {
text[--len] == '\0';
}
for (i = j = 0; i < len; i ) {
c = text[i];
if (c != ' ') {
if (lastc == ' ') {
if (word_count > 0) {
// output a space between words
text[j ] = ' ';
}
word_count ;
}
text[j ] = c; // copy the non space character
}
lastc = c;
}
text[j] = '\0'; // set the null terminator
printf("kelime say?s?: %d\n", word_count);
printf("??kt?: %s\n", text);
total_words = word_count;
}
fclose(fp);
printf("\ntoplam kelime say?s?: %d\n", total_words);
return 0;
}
請注意代碼中的一個愚蠢錯誤:if (i = 0 && text[i] == ' ')被決議為if ((i = (0 && (text[i] == ' '))) != 0)始終為 false 并設定i為 value 0。C 運算式語法非常強大,但有些容易出錯和令人困惑。我建議您使用-Wall或-Weverything作為編譯器選項,讓編譯器警告潛在的錯誤。
同樣,你不應該寫if (1<i<z && ...: 1<i<zis parsed as 1<(i<z)which 總是假的。你必須寫1 < i && i < z或更慣用的i > 1 && i < z
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/379878.html
