Thread: Counting 1's in a string of 1's and 0's

  1. #1
    Registered User
    Join Date
    Feb 2006

    Counting 1's in a string of 1's and 0's


    I'm currently working on a project in an engineering design course. Unfortunately, my system requires some programming, something which I haven't focused on at all in my studies.

    For one part of my project, I need to write a program in C which I will put on a PLD. The objective of the program is to receive several strings of 1's and 0's (12 digits per string), count the number of 1's in each strings, and then decide which string had the most 1's.

    The comparison at the end seems easy enough, but I'm having trouble figuring out how to add up the contents of the string. If anyone could offer me help or guidance on how to implement such a task in C, it would be greatly appreciated.

    Thank you in advance.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    One idea:
    int foo(const char *text)
       int i, ones = 0;
       for ( i = 0; text[i] != '\0'; ++i )
          ones += text[i] - '0';
       return ones;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. producing a matrix of 1's and 0's
    By emj83 in forum C Programming
    Replies: 5
    Last Post: 02-24-2009, 05:10 AM
  2. Improve Performance of String comparison
    By yagmai in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 12:11 PM