我在C做練習,但我有一個問題,當我和我想重複cicle(做while)時,事實上,如果我輸入1程序再次從頂部開始,但它不止於gets(testo);
。我嘗試了很多方法來解決錯誤,沒有解決方案,誰能幫助我?字符串,得到和做
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
main(){
int cch, cw, i, j, w, ord, f; //counter and index
char testo[80];
char alfa[50][25];
char swap[25];
do{
cch=0;
cw=0;
j=0;
w=0;
f=0;
for(i=0;i<80;i++){
testo[i]='\0';
}
printf("Write the text:\n");
gets(testo);
//initialization 2d array
for(i=0;i<50;i++){
for(j=0;j<25;j++){
alfa[i][j]='\0';
}
}
j=0;
//Count word and characters
if(testo[0]!='\0'){
cw=1;
for(i=0;testo[i]!='\0';i++){
cch++;
if(testo[i]==' '){
cw++;
j++;
}
}
}
if(cch==j){
printf("You haven't written any word\n\n");
}
else{
//Don't count double space
for(i=0;i<cch;i++){
if(testo[i]==' ' && testo[i+1]==' '){
cw--;
}
}
//Don't count as word if the text start with a space
if(testo[0]==' '){
cw--;
w--;
}
printf("\nThe text is composed by %d characters\n", cch);
printf("The text is composed by %d words\n", cw);
if(cw>0){
printf("\nUsed words:\n");
for(j=0;j<cch;j++){
if(testo[j]==' ' && testo[j+1]==' '){
//nothing to do
}
else{
if(testo[j]!=' '){
alfa[w][f]=testo[j];
f++;
}
else if(testo[j]=='\0'){
alfa[w][f]='\0';
f=0;
w=0;
}
else{
alfa[w][f]='\0';
w++;
f=0;
}
}
}
for(i=0;i<cw;i++){
printf("%d> %s\n", i+1, &alfa[i]);
}
//order
f=1;
printf("\nWords used in alphabetical order:\n");
while(f==1){
f=0;
for(i=0;i<cw-1;i++){
ord=strcmp(alfa[i],alfa[i+1]);
if(ord>0){
strcpy(swap,alfa[i]);
strcpy(alfa[i],alfa[i+1]);
strcpy(alfa[i+1],swap);
f=1;
}
}
}
for(i=0;i<cw;i++){
printf("%d> %s\n", i+1, alfa[i]);
}
}
}
printf("\nDo you want write another text? (1=yes) -> ");
scanf("%d", &j);
}while(j==1);
}
我知道這是不是爲代碼在此刻非常優化並具有其他錯誤,但我對這個有問題。
謝謝。
PS:該代碼是在OpenVMS上
謝謝,我會試試。對於第三點,我忘了在翻譯過程中放置%d;) – Mitro 2013-03-14 14:23:58
@AlessioMTX - 是的,我認爲缺少'%d'的東西在翻譯時被遺忘了,似乎很明顯是錯過了。祝你好運,讓我知道你是否有任何後續問題。再見。 – Mike 2013-03-14 14:46:03
解決:D謝謝;) – Mitro 2013-03-14 20:37:49