finding MSB in a string

This is a discussion on finding MSB in a string within the C Programming forums, part of the General Programming Boards category; hi, i'd like to find the most significant byte or bit within a string of chars. the size of the ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    108

    finding MSB in a string

    hi,

    i'd like to find the most significant byte or bit within a string of chars. the size of the string is not set and can vary.

    my char is something like:

    Code:
    unsigned char mystring = "WWW003674574574asdgasdgas000000";
    and i'd like to find the HIGHEST occurance of a non zero character (in this case s i think) and display it as the address it is contained in. the data is written into the string after bieng received from a port starting at "WWW"

    i'll then search through another character and display the diference between the two strings as a number of bytes.

    is there a specific function that will do it or do i need to use
    Code:
    if
    loops to find occurances of non zero characters?

    thanks
    Last edited by shoobsie; 11-08-2005 at 01:42 AM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    the way I would approach the problem is to create an array of 255 integers, which represent each of the 255 possible characters, initialize each element to 0, then use each character of the string as index into the array. When done, just check the array for hightest number.
    Code:
    int array[255] = {0};
    char *string = "abs00233aabbsssssww";
    int i;
    for(i = 0; string[i]; i++)
      array[string[i]]++;
    // now just search array for highest count

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by Ancient Dragon
    the way I would approach the problem is to create an array of 255 integers, which represent each of the 255 possible characters, initialize each element to 0, then use each character of the string as index into the array. When done, just check the array for hightest number.
    Code:
    int array[255] = {0};
    char *string = "abs00233aabbsssssww";
    int i;
    for(i = 0; string[i]; i++)
      array[string[i]]++;
    // now just search array for highest count
    thanks, i'll try that.

    its actually a hex string so i guess the number of possible characters is reduced

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  2. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21