Finding smallest and largest string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crystal2005
    New Member
    • Apr 2007
    • 44

    Finding smallest and largest string

    I am writing a program that receive maximum of 25 line of string each has 20 characters maximum. The program will print the smallest and the largest string. However the following program gives me Segmentation fault (core dumped) :(( It looks simple but i have no idea what went wrong....

    Can anyone help me out??

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define MAX_INPUT 25
    #define MAX_CHAR 20
    
    int main(void)
    {
    	int t, i, j;
    	
    	char text[MAX_INPUT][MAX_CHAR];
    	
    	char smallest[0][MAX_CHAR], largest[0][MAX_CHAR];
      
    	printf("Enter 4 characters to quit. \n");
    
    	for(t=0; t<MAX_INPUT; t++)
    	{
    		printf("Enter word %d: ", t);
    		gets(text[t]);
    		if(strlen(text[t])==4) break;
    	}
    
    	
    	for(i=0; i<t; i++)
    	{
    		for(j=0; text[i][j]; j++)
    		{
    				if(text[i][j]<smallest[0][MAX_CHAR])
    				{
    					text[i][j]=smallest[0][MAX_CHAR];
    				}
    				if(text[i][j]>largest[0][MAX_CHAR])
    				{
    					text[i][j]=largest[0][MAX_CHAR];
    				}
    		}
    	}
    
    	printf("Smallest word: %s\n", smallest[0][MAX_CHAR]);
    	printf("Largest word: %s\n", largest[0][MAX_CHAR]);
      
    	return EXIT_SUCCESS;
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You don't have to remember all those strings; all you have to remember are the smallest and largest strings so far. After having read the first string that string is the smalles and largest string so far.

    kind regards,

    Jos

    Comment

    • crystal2005
      New Member
      • Apr 2007
      • 44

      #3
      Originally posted by JosAH
      You don't have to remember all those strings; all you have to remember are the smallest and largest strings so far. After having read the first string that string is the smalles and largest string so far.

      kind regards,

      Jos
      Sorry Jos, what do you mean the first string is the smallest and largest string so far??

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by crystal2005
        Sorry Jos, what do you mean the first string is the smallest and largest string so far??
        Suppose you haven't read any strings yet; you don't know what the smallest or largest strings so far are; suppose you have read one string; it has to be the smallest and largest string read so far.

        kind regards,

        Jos

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #5
          text[i][j]=smallest[0][MAX_CHAR];

          What do you try to accomplish here?

          Comment

          • crystal2005
            New Member
            • Apr 2007
            • 44

            #6
            Originally posted by newb16
            text[i][j]=smallest[0][MAX_CHAR];

            What do you try to accomplish here?
            I want to store the smallest array of string. But the code doesn't seem that way.

            Comment

            • newb16
              Contributor
              • Jul 2008
              • 687

              #7
              char largest[0][MAX_CHAR] is a wrong way to store string - sizeof(largest) on my machine returns 0, because it's an array of zero arrays of N chars. However largest[0][MAX_CHAR] is located at 25 bytes from its beginning, overlapping some other variable. Also, you never assign anything to 'smallest' but print it after all.

              Comment

              • crystal2005
                New Member
                • Apr 2007
                • 44

                #8
                After trying for quite sometime, it does work... but my code only take one character comparison in front. Any suggestion to compare probably up to 2 or 3 characters for each string??

                Code:
                #include<stdio.h>
                #include<stdlib.h>
                #include<string.h>
                
                #define MAX_INPUT 25
                #define MAX_CHAR 20
                
                int main(void)
                {
                	int t, i;
                	
                	char text[MAX_INPUT][MAX_CHAR];	
                  
                	char* largestString;
                	char* smallestString;
                	unsigned int largestStringASCII;
                	unsigned int smallestStringASCII;
                
                	unsigned int largestValue = 0;
                	unsigned int smallestValue = 127;	//ASCII max No.
                		
                	printf("Enter 4 characters to quit. \n");
                
                	for(t=0; t<MAX_INPUT; t++)
                	{
                		printf("Enter word %d: ", t);
                		gets(text[t]);
                		if(strlen(text[t])==4) break;
                	}
                			
                	
                	for(i=0; i<=t; i++)
                	{		
                		largestStringASCII = text[i][0];
                		if(largestStringASCII>largestValue)
                		{
                			largestValue = largestStringASCII;
                			largestString = text[i];
                		}
                	}
                	
                	for(i=0; i<=t; i++)
                	{
                		smallestStringASCII = text[i][0];
                		if(smallestStringASCII<smallestValue)
                		{
                			smallestValue = smallestStringASCII;
                			smallestString = text[i];
                		}
                	}
                	
                	printf("Smallest word: %s\n", smallestString);
                	printf("Largest word: %s\n", largestString);
                  
                	return EXIT_SUCCESS;
                }

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Why don't you use the strcmp( ... ) function? It was made for these purposes.

                  kind regards,

                  Jos

                  Comment

                  Working...