PROBLEM 32:
Write a C program to convert numeric string to an int of same value?
For Example, if you read a string “1524” then an integer variable in your program should be
assigned 1524 as an integer.
CODE:
#include<stdio.h>
main()
{
// Variable declaration//
char a[10];
int i,sum=0;
/*Input*/
printf("Enter any integer:");
gets(a);
/* actual Working */
for(i=0;a[i]!='\0';i++) //this loop for cheak array index is less than 48 and greater than 57
{
if(a[i]<48 || a[i]>57)
{
printf("string cannot convert to integer\n");
break; //if condition is true so this loop will be break
}
else //if condition is not true
{
sum=sum*10+a[i]-48; //here sum multiply by 10 and than array index subtaract 48 and add sum
}
}
if(a[i]>57) //if a[i] greater tha 57 so its wrong
{
printf("Please enter integer value");
}
else
{
printf("string succesuuly convert to integer:%d",sum);
}
return 0;
}
Program Output:
PROBLEM 31:
Write a C program to find a sub-string in a string.
Sample Input: you just blew my mind
Substring to search: my
Sample Output: Substring found.
CODE:
#include<stdio.h>
main()
{
// Variable declaration//
char a[100];
char b[100];
int i=0,j=0,count=0,count1=0;
/*Input*/
printf("Enter string:");
gets(a);
printf("Enter substring to found:");
gets(b);
/* actual Working */
for(i=0;b[i]!='\0';i++)//here i count total substring words
{
count=count+1;
}
for(i=0;a[i]!='\0';i++)//here I compare string1 value with string2
{
if(a[i]==b[j])// if it match so increment in count1 will perform
{
count1++;
j++;
}
if(count1==count)//if substring words equal to count1 so loop will break
{
printf("Substring found");
break;
}
}
if(count1!=count)// if count1 not equal
{
printf("no Substring found");
}
return 0;
}
Program Output:
For more problems click here
PROBLEM 30:
A string is said to be complete if it contains all the characters from a to z.
For a user-entered string, check if it is complete or not.
Sample Input: The quick brown fox jumps over a lazy dog
Sample Output: The entered string is complete.
CODE:
#include<stdio.h>
main()
{
// Variable declaration//
int index=0,count1[26]={0},count=0,i;
char str[50];
char temp;
/*Input*/
printf("Enter string:\n");
gets(str);
for(i=0;str[i];i++) //this loop will convert uppercase alphabets to lowercase
{
if(str[i]>=65 && str[i]<=90) //here if str array greater than 64 and less than 91 so condition will work
{
temp=str[i];
str[i]=temp+32; //here i add 32 to each array example:if array equal to 65 than add 32
}
}
/* actual Working */
while (str[index] != '\0') //this loop will run until array index not equal to null
{
if (str[index] >= 'a' && str[index] <= 'z') //here I cheak that if array index >=a and <= to z
count1[str[index]-'a']++; //then count1 subtract by index value and increment in count1
index++;
}
for (index = 0; index < 26; index++)
{
if (count1[index] != 0)
count=count+1;
}
if(count==26) //if count equal to 26 then this condition will run
{
printf("The entered string is complete");
}
else
{
printf("The entered string is not complete");
}
return 0;
}
Program Output:
PROBLEM 29:
Write a C program to delete the all vowels from a user-entered string. You
are required to delete the vowels from the string. Just not printing them is not a valid answer.
Sample Input1:Hello World (String Length=11)
Sample Output1: Hll Wrld (String Length=8)
Sample Input2: Apple (String Length=5)
Sample Output2:ppl (String Length=3)
CODE:
#include<stdio.h>
main()
{
// Variable declaration//
char a[25];
char b[25];
int i,j=0;
/*Input*/
printf("Enter string:");
gets(a);
/* actual Working */
for(i=0;a[i]!='\0';i++)//this loop will rununtil not equal to null
{
if(a[i]=='a' || a[i]=='e' || a[i]=='i' || a[i]=='o' || a[i]=='u')//if array index is equal to a or e or i or u or s then condition will run
{
a[i]='#';//here i replace array index value with #
}
}
printf("After remove vowels\n");
printf("Your string is:");
for(i=0;a[i]!='\0';i++)
{
if(a[i]>='a' && a[i]<='z' || a[i]>='A'&& a[i]<='Z' || a[i]==' ')//this will cheak that array index greater than a and less than z
{
printf("%c",a[i]);//this will print if condition is true
}
}
return 0;
}
Program Output:
PROBLEM 28:
Write a program that reads full names, one per line, and then prints them in
the standard telephone directory format. For example,
Sample Input1: George Frederic Handel
Sample Output1: Handel, George F.
Sample Input2: Joseph Haydn
Sample Ouput2: Haydn, Joseph
Sample Input3: Johann Christian Bach
Sample Output3: Bach, Johann C.
Your program should ask user name and print output repeatedly till user press any sentinel value
(you may select any sentinel here)
CODE:
#include<stdio.h>
main()
{
// Variable declaration//
char a[25];
char b[25];
int i=0,j,k=0,count=0,count1=0,space=0,c,m=0;
/*Input*/
printf("if you want to exit enter #\n");
do{
for(j=0;j<25;j++)// here i intail all array index with 0
{
b[j]=0;
}
printf("\n");
printf("Enter String to add in telephone dictionary:");
gets(a);
/* actual Working */
if(a[0]=='#') //if input equal to # so loop will break
{
break;
}
for(i=0;a[i]!='\0';i++)//this loop will count total character
{
count=count+1;
if(a[i]==' ') //if space is find in array so this condition will run
{
b[k]=count;
k++;
m++;
}
}
for(j=0;b[j]!='\0';j++)//this loop will save b[j] value in c varaible
{
c=b[j];
}
for(j=c;a[j]!='\0';j++)
{
printf("%c",a[j]);
}
printf(",");
printf(" ");
if(a[j]=='\0' && m>1)
{
for(j=0;j<=b[0];j++)
{
printf("%c",a[j]);
}
printf(".");
}
else
{
for(j=0;j<b[0];j++)
{
printf("%c",a[j]);
}
}
//here I again intail every varaible with zero because loop will start from begaining
i=0;
j=0;
k=0;
count=0;
count1=0;
space=0;
c=0;
m=0;
}while(a[0]!='#');
return 0;
}
Program Output:
PROBLEM 27:
Create a fraction calculator that takes input of two fraction and perform
the operation select by the user
First create two char array that is used to take the input in fraction from user and then user input the
operation to be performed like (+,-,/,*). your program should print the menu like below
Sample Input: Enter The first fraction value : 2/5
Sample Output: Enter The second Fraction Value : 2/5
CODE:
#include<stdio.h>
main()
{
// Variable declaration//
char a[3];
char b[3];
char c[3];
int i,sum=0,j=0,choice,sum1,temp;
float total,sum2,sum3;
for(i=0;i<3;i++) // here I intail array a and b with null
{
a[i]='\0';
b[i]='\0';
}
/*Input*/
printf("Enter 1st fraction:");
gets(a);
/* actual Working */
for(i=0;a[i]!='\0';i++)// this loop will convert asii to integer
{
if(a[i]>=48 && a[i]<=57 ) //if a arrry greater than 48 and less than 57 than this conition will run
{
c[j]=a[i]-48; //here I subtract 48 from a element and save it in c element
j++;
}
}
printf("Enter second fraction:");
gets(b);
for(i=0;i<3;i++)//this loop will work as same which I work ar above
{
if(b[i]>=48 && b[i]<=57)
{
b[i]=b[i]-48;
}
}
printf("Press 1 for Addition\nPress 2 for Substraction\nPress 3 for Multiplication\nPress 4 for division\n");
scanf("%d",&choice);//here what you want to perform
if(choice==1)
{
if(c[1]!=b[2])//if first fraction denominator is not same as second denominator so this condtion will work
{
b[0]=b[0]*c[1];
c[0]=c[0]*b[2];
sum=c[0]+b[0];
b[2]=c[1]*b[2];
c[1]=c[1]*b[2];
}
else{
sum=c[0]+b[0];
}
if(sum%2==0 && c[1]%2==0) //this condition will cheak that denominator or numerator divide by 2 or not
{
sum=sum/2;
b[2]=b[2]/2;
}
if(sum%3==0 && c[1]%3==0) //this condition will cheak that denominator or numerator divide by 3 or not
{
sum=sum/3;
b[2]=b[2]/3;
}
printf("Result:%d/%d",sum,b[2]);
}
else if(choice==2) //if choice equal to substraction so this condtion will perform
{
if(c[1]!=b[2])
{
c[0]=c[0]*b[2];//here I set first fraction NUMERATOR multiply by second fraction denominator
b[0]=b[0]*c[1];
temp=c[1];
c[1]=c[1]*b[2];
b[2]=temp*b[2];
sum=c[0]-b[0];
}
else{
sum=c[0]-b[0];
}
if(sum%2==0 && c[1]%2==0)
{
sum=sum/2;
c[1]=c[1]/2;
}
if(sum%2==0 && c[1]%2==0)
{
sum=sum/2;
c[1]=c[1]/2;
}
if(sum%3==0 && c[1]%3==0)
{
sum=sum/3;
c[1]=c[1]/3;
}
printf("Result:%d/%d",sum,c[1]);
}
else if(choice==3)
{
sum=c[0]*b[0];
sum1=b[2]*c[1];
printf("Result:%d/%d",sum,sum1);
}
else if(choice==4)
{
temp=b[0];
b[0]=b[2];
b[2]=temp;
c[0]=c[0]*b[0];
c[1]=c[1]*b[2];
printf("%d",c[0]);
total=c[0]/c[1];
printf("Result:%.2f",total);
}
else
{
printf("Invalid choice");
}
return 0;
}
Program Output: