Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #define MAX 1024
- int numOcc(char * str, char srch) {
- int i, Count = 0;
- for(i = 0; i < strlen(str); i++) {
- if(str[i] == srch)
- Count++;
- }
- return Count;
- }
- void toBin(char * num, char * dest) {
- unsigned int pNum = atoi(num);
- int i, bin[MAX];
- for(i = 0; i < MAX; i++)
- bin[i] = -1;
- i = MAX - 1;
- strcpy(dest, "");
- while(pNum > 0) {
- bin[i] = pNum%2;
- pNum /= 2;
- i--;
- }
- for(i = 0; i < MAX; i++) {
- if(bin[i] == -1)
- continue;
- char buffer[2];
- sprintf(buffer, "%d", bin[i]);
- strcat(dest, buffer);
- }
- }
- int main(void) {
- int i;
- char nums[MAX][11];
- for(i = 0; i < MAX; i++)
- strcpy(nums[i], "");
- i = 0;
- while(1) {
- char aux[10 + 1];
- scanf("%s", aux);
- if(strcmp(aux, "0") == 0)
- break;
- strcpy(nums[i], aux);
- i++;
- }
- i = 0;
- while(1) {
- if(strcmp(nums[i], "") == 0)
- break;
- char Bin[MAX];
- toBin(nums[i], Bin);
- printf("The parity of %s is %d (mod 2).\n", Bin, numOcc(Bin, '1'));
- i++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement