Question

I'm doing an exercise in C with strings, I have to order some words taken by a text.

#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");
        }
}

The problem is that if there are two identical words, and the words are more than 2, I have a loop and I don't have any result, what should I do? Tested on OpenVMS. Thank you.

PS: I know that there are plenty of bug at the moment, but I'm having problem to solve this.

Was it helpful?

Solution

if(ord>-1){
    /* ... */
} 

If both words are the same strcmp will return 0. This will swap both words until your next power bill comes in and you shut your program down. Instead, check whether the result is greater than zero:

if(ord > 0){
    /* ... */
}

See also: strcmp

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top