Toc Exp
Toc Exp
#include <stdio.h>
int main() {
int number;
return 0;
}
5) Design a machine which accepts string having equal no. of 1’s and 0’s.
#include <stdio.h>
#include <string.h>
// Function to check if the string has equal number of 1's and 0's
int check_equal_ones_zeros(const char *str) {
int stack = 0; // We use an integer as a stack counter
int main() {
char input[100];
// Input a string from the user
printf("Enter a binary string (containing only 1's and 0's): ");
scanf("%s", input);
return 0;
}
6) Design a C program for creating a machine which count number of 1’s and 0’s in a
given string.
#include <stdio.h>
#include <string.h>
int main() {
char input[100]; // To store the input string
int count_ones = 0, count_zeros = 0; // To store counts of '1's and '0's
return 0;
}
int main() {
char binary[100];
return 0;
}
8)#include <stdio.h>
#include <string.h>
// Function to increment a binary number by 1
void incrementBinary(char binary[], int length) {
int carry = 1; // Start with a carry of 1 to add
// Traverse the binary number from the least significant bit to the most
significant bit
for (int i = length - 1; i >= 0; i--) {
if (binary[i] == '1' && carry == 1) {
binary[i] = '0'; // If bit is '1' and carry is '1', set bit to '0' and
carry stays
} else if (binary[i] == '0' && carry == 1) {
binary[i] = '1'; // If bit is '0' and carry is '1', set bit to '1' and
clear carry
carry = 0;
}
}
// If carry is still 1 after looping through the entire string, prepend '1' to
the result
if (carry == 1) {
printf("1"); // Print an extra '1' if the carry overflowed
}
}
int main() {
char binary[100];
return 0;
}