Thread: Finding MAX or MIN within a string

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    3

    Question Finding MAX or MIN within a string

    I am new to C, just experimenting few things. I am trying to find the maximum number or the last number within a string, exp: if I entered 1234 I should get 4 back, but the code below returns the whole thing: 1234

    Is there a function (maybe called "MAX") I can use to retrieve the last number whithin a string?

    Thanks for your input in advance.

    -Dale
    [email protected]

    Code:
    #include <stdio.h>
    #include <string.h>
    main()
    { 
    	int a, i, j;
    	char x[100];
    	
    	printf("Please give me a string: ");
    	scanf("%s",x);
    	printf("\n");
    
    	i = 0;
    	while (x[i] != NULL)
    	{
    		x[i] = x[i];
    		++i;
    	}
    	
    	printf("The maximum number within this string is %s ",x);
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    How about :
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char  buf[] = "1234";
      int i;
      
      for (i = 0; buf[i] != '\0'; i++)
      {
      }
      
      i--;
      
      printf ("Last character in string is %c\n", buf[i]);
      
      return(0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
      char  buf[] = "1234";
      char max = 0;
      int i;
      
      for (i = 0; buf[i] != '\0'; i++)
        max = ( isdigit( buf[ i ] ) && buf[ i ] > max ? buf[ i ] : max );
        
      printf ("Biggest character in string is %c\n", max);
      
      return(0);
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
    {
      char max='\0', min='\0';  /* Initalized them to characters thate the user can't input */
      int len, count;
      char buff[20];
    
      scanf("%s", buff);
    
      len = strlen(buff); /* How long is the string */
    
      for (count=0; count<len; count++) /* Step through the string */
      {
        if ( buff[count] < '1' || buff[count] > '9') /* If the characters are not between 1 and 9 then skip them */
          continue;
    
        if (max == '\0')
        {
          max = min = buff[count]; /* if max is still the inital value then assign the character to min and then to max */
          continue; /* skip the rest of the loop and go on to next character */
        }
        if ( max < buff[count] ) /* If max is lower then the character its no longer the max */
          max = buff[count];
        if ( min > buff[count] ) /* if min is greater then the character its no longer the min */
          min = buff[count];
      }
    
      printf("The max and min of %s is %c and %c\n", buff, max, min);
      return 0;
    }

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    max = ( isdigit( buf[ i ] ) && buf[ i ] > max ? buf[ i ] : max );
    You trying to kill the poor kid XSquared?

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Hey, it works. (I think)

    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Registered User
    Join Date
    Feb 2004
    Posts
    3
    Thank you guys for your prompt response.

    I see that you hard coded 1234, but instead, I'd like to ask for the user input, he/she can enter any numbers from 1 to 9, the pgm should retrieve the MAX based on the input from the user..

    Thank you again.
    -Dale

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    3
    Thantos, your code worked beautifully, it is way to cool

    Thank you everyone for your help, this is a cool Forum, I will let all my friends know about.. You guys rock!

    -Dale

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Remember its more important to know how it works then just having it worked.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I am trying to find the maximum number or the last number within a string
    This is two different requirements. Which is it your after doing?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int main( void )
    {
        char buf[BUFSIZ] = {0};
        char *p[10] = {0};
        int x;
    
        printf("Enter a string containing digits: ");
        fgets( "%s", BUFSIZ, stdin );
    
        for( x = 0; x < 10; x++ )
            p[x] = strrchr( buf, '0'+x );
    
        for( x = 9; x > -1; x-- )
            if( p[x] )
            {
                printf("%c is the highest digit in your string.\n", '0'+x );
                break;
            }
        if( x < 0 )
            printf("There were no digits.\n");
    
        return 0;
    }
    You could do the same thing but do some pointer comparison to see which one occurred last.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Memory leak - need help finding
    By ChadJohnson in forum C++ Programming
    Replies: 8
    Last Post: 04-06-2005, 07:26 PM
  3. Game of life
    By JoshR in forum C++ Programming
    Replies: 30
    Last Post: 04-03-2005, 02:17 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM