Question
( Please help me to solve this using C programming) Thank you! I'll be sure to respond with a rating! Also! at the bottom I
( Please help me to solve this using C programming) Thank you! I'll be sure to respond with a rating!
Also! at the bottom I will include one way to solve this, could you please show me alternative code to solve this problem?
Write a program that can:
(1) count the number of capital letters in an input string using C.
(2) If two characters (input) differ by a single bit.
- You should add text prompts to the user displaying the results, so you will need to create a main loop. You may also submit both functions in a single c file (preferred). One C file that calls the functions you create to solve these prompts works wonderfully.
- This section focuses on standard input and output in C, and it gives you exposure to bit operations in the language. These are essential tools for systems level programmers, and people using C usually focus on the system/optimal approach.
One way to solve:
#include#include // Function to count capital letters int countCapitalLetters(char str[], int len){ // Count to store number of capital letters int count = 0; // Iterating through the entire character array for(int i=0;i = 65 && str[i] <= 90){ // If yes, increase count by 1 count = count + 1; } } // Return the value of counr return count; } // Function to check if the numbers differ by a single bit int difference(int num1, int num2){ // Caclulate xor // if xor (represented in binary) contains only one bit // Then they differ by single bit int xorOfNumbers = num1 ^ num2; // Which means it should be a power of 2 // Checking for power of 2 while(xorOfNumbers > 1){ if(xorOfNumbers % 2 != 0){ return 0; }else{ xorOfNumbers = xorOfNumbers / 2; } } // It is a power of 2 if(xorOfNumbers == 1) return 1; else return 0; } int main(){ // Taking string input char str[100]; int num1, num2; printf("Enter the string: "); gets(str); // Calling function printf("The number of capital letters in the string are %d ", countCapitalLetters(str, strlen(str))); printf(" Enter two numbers: "); // Taking integers input scanf("%d %d", &num1, &num2); // Calling function printf("Do they differ by a single bit? %d ", difference(num1, num2)); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started