0
C로 문자열로 연습하고 있는데, 텍스트로 찍은 단어를 주문해야합니다.알파벳 순서 2d 배열
#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");
}
}
문제가 두 개의 동일한 단어와 단어가 둘 이상있는 경우 어떻게해야합니까, 내가 루프를 가지고 있고 어떤 결과를 필요가 없다는 것입니다? OpenVMS에서 테스트되었습니다. 감사합니다.
추신 : 현재 많은 버그가 있음을 알고 있지만 해결해야 할 문제가 있습니다. 두 단어 인 경우
strcmp
당신이 바보 같은 오류가 나는 – Mitro@AlessioMTX을했다는 :) 감사합니다 모든 사람에게 발생합니다. 추가 설명 :'strtok'은 단어 계산이 훨씬 쉬워지고'strlen'과 함께 사용될 수 있습니다. – Zeta
조언 해 주셔서 감사합니다;) – Mitro