0
我正在用字符串在C中鍛鍊,我不得不點一些文字。字母順序一個二維數組
#include <stdio.h>
#include <string.h>
main(){
int cch=0, cw=0, i, j, w=0, ord=0, f=0; //counter and index
char testo[80];
char alfa[50][25];
char swap[25];
printf("Write the test:\n");
gets(testo);
if(testo[0]!='\0'){
cw=1;
for(i=0;testo[i]!='\0';i++){
cch++;
if(testo[i]==' '){
cw++;
}
}
}
for(i=0;i<cch;i++){
if(testo[i]==' ' && testo[i+1]==' '){
cw--;
}
}
if(testo[0]==' '){
cw--;
w--;
}
printf("\nIn the test there are %d characters\n", cch);
printf("In the test there are %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("\nWord 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>-1){
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]);
}
}
else{
printf("You haven't written any word.\n");
}
}
的問題是,如果有兩個相同的話,而這句話是超過2個,我有一個循環,我沒有任何結果,我該怎麼辦? 在OpenVMS上測試。 謝謝。 PS:我知道現在有很多bug,但是我有問題要解決這個問題。
謝謝:)那個愚蠢的錯誤,我做了 – Mitro 2013-03-10 08:50:38
@AlessioMTX:發生在每個人。進一步的評論:'strtok'會讓你的字數更容易計算,並且可以和'strlen'一起使用。 – Zeta 2013-03-10 08:51:49
感謝您的建議;) – Mitro 2013-03-10 08:53:02