Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

i need to fix this code so it can compare the two strings and print the bigger according to the ascii

0 votes
asked Aug 5 by عبد السلام محمد (120 points)
#include <stdio.h>
#include <string.h>

    int main(){
        int count=0;
        int count1=0;
        char str[101];
        gets(str);
            for(int i=0;i<=101;i++){
                if(str[i]==' ')
                count++;
                
            }
            int wordsnr=count+1;
            char words[wordsnr][30];
            int word=0, ch=0;
                for(int i =0;i<=(strlen(str));i++){
                    if(str[i]==' ' || str[i]=='\0'){
                        words[word][ch]='\0';
                        word++;
                        ch=0;
                        
                    }
                    else {
                        words[word][ch]=str[i];
                        ch++;
                        
                }
    }
    int index =0;
    int c;
    int longest=strlen(words[0]);
    for (int i =1;i<word;i++){
        if(strlen(words[i])>longest){
            if(strlen(words[i])==longest){
                c= strcmp(words[i],words[index]);
                
            }
            longest=strlen(words[i]);
            if(c!=(-1))
            printf("%s",words[i]);
            else printf("%s",words[index]);
        }
       
        }
         printf("%s",words[index]);
    }

1 Answer

0 votes
answered Sep 22 by Peter Minarik (34,450 points)

Problem #1 - depriceted function

gets(str);

This function is deprecated. It shouldn't be used anymore. Use fgets() instead:

fgets(str, sizeof(str), stdin);

Problem #2 - buffer overrun

for(int i=0;i<=101;i++){

You are likely to read memory garbage there. You should stop reading when you reach the end of the string.

Problem #3 - dead code

The following code will never run:

if(strlen(words[i])>longest){
    if(strlen(words[i])==longest){
        c= strcmp(words[i],words[index]);
    }
    // ...
}

The reason is that if a > b, then a clearly cannot == b.

Problem #4 - uninitialized variable

The value c is uninitialized. It has no initial value and setting it (see above) is part of a dead code, a conditional one, it should have been initialized anyway.

Therefore when you're trying to read it, it will likely never be equal to -1.

Performance #1 - O(n^2) complexity

You shouldn't call strlen() all the time, especially not in a for loop a it is evaluated every iteration making the problem O(n^2) while it could be just O(n) complex.

So instead of 

for(int i =0;i<=(strlen(str));i++){

you could first calculate and save the length and use it in the loop:

size_t length = strlen(str);
for (size_t i = 0; i <= length; i++) {
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...