Q1.
Write a function to solve the following equation a3 + a2b + 2a2b + 2ab2 + ab2 + b3
Write a program to accept three values in order of a, b and c and get the result of the above equation.
solution->
#include <stdio.h>
int main() {
int a, b, c;
int result;
// Input values
printf("Enter values for a, b, and c: ");
scanf("%d %d %d", &a, &b, &c);
// Compute (a + b)^3
result = (a + b) * (a + b) * (a + b);
// Output the result
printf("Result of the equation (a + b)^3 is: %d\n", result);
return 0;}
Q2. You have write a function that accepts, a string which length is “len”, the string has some “#”, in it
you have to move all the hashes to the front of the string and return the whole string back and print
it
Example :-
Input:
Move#Hash#to#Front
Output:
###MoveHashtoFront
solution->
#include <stdio.h>
#include <string.h>
void moveHashToFront(char str[]) {
int i, j = 0;
int len = strlen(str);
char temp[100];
// Copy non-# characters to temp
for (i = 0; i < len; i++) {
if (str[i] != '#') {
temp[j++] = str[i];
}
// Fill the original string with #s at the beginning
for (i = 0; i < len - j; i++) {
str[i] = '#';
// Add the non-# characters back
for (int k = 0; k < j; k++) {
str[i++] = temp[k];
str[i] = '\0'; // Null terminate
printf("Output: %s\n", str);
int main() {
char str[100] ;
printf(“enter the string to be :”);
scanf(“%s”,str);
moveHashToFront(str);
return 0;
}
Q3.Written test have a coding question, wherein the students are given a string with multiple characters
that are repeated consecutively. You’re supposed to reduce the size of this string using mathematical
logic given as in the example below :
Input :
aabbbbeeeeffggg
Output:
a2b4e4f2g3
Input :
abbccccc
Output:
ab2c5
solution->
#include <stdio.h>
void compress(char str[]) {
int i = 0, count;
while (str[i] != '\0') {
printf("%c", str[i]);
count = 1;
// Count consecutive characters
while (str[i] == str[i + 1]) {
count++;
i++;
// Print count if more than 1
if (count > 1) {
printf("%d", count);
i++;
printf("\n");
int main() {
char input[10] ;
printf("enter the string :");
scanf("%s",input);
compress(input);
return 0;
}
Q.4: Consider a string, S, that is a series of characters, each followed by its frequency as an integer. The
string is not compressed correctly, so there may be multiple occurrences of the same character. A
properly compressed string will consist of one instance of each character in alphabetical order followed
by the total count of that character within the string.
Input: "a3b5c2a2"
Output: "aaabbbbbcc"
solution->
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void expandString(char str[]) {
int count[26] = {0}; // for a-z
int i = 0;
while (str[i] != '\0') {
if (isalpha(str[i])) {
char ch = str[i];
i++;
int num = 0;
// Read the number after character
while (isdigit(str[i])) {
num = num * 10 + (str[i] - '0');
i++;
count[ch - 'a'] += num;
// Print expanded characters in order
for (int j = 0; j < 26; j++) {
for (int k = 0; k < count[j]; k++) {
printf("%c", 'a' + j);
printf("\n");
int main() {
char input[] ;
printf("enter the string :");
scanf("%s",input);
expandString(input);
return 0;