0% found this document useful (0 votes)
7 views16 pages

Recursive Binary Conversion and String Manipulations

The document contains a series of C programming tasks and their implementations, covering various topics such as recursion, string manipulation, bitwise operations, and number properties. Each task includes a brief description, followed by the corresponding C code that demonstrates the solution. The tasks range from converting decimal to binary, reversing strings, counting words, to more complex operations like checking for strong numbers and manipulating bits.

Uploaded by

Vinay Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

Recursive Binary Conversion and String Manipulations

The document contains a series of C programming tasks and their implementations, covering various topics such as recursion, string manipulation, bitwise operations, and number properties. Each task includes a brief description, followed by the corresponding C code that demonstrates the solution. The tasks range from converting decimal to binary, reversing strings, counting words, to more complex operations like checking for strong numbers and manipulating bits.

Uploaded by

Vinay Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1) Print the decimal number in binary in recursive fun?

#include<stdio.h>
void print(int num)
{
if(num>1)
{
print(num/2);
}
printf("%d",num%2);

}
void main()
{
int n;
printf("enter the number\n");
scanf("%d",&n);
print(n);
}
Iteration Explanation
Initial Call:

The user enters 13.


main () calls printBinary (13).
First Call to printBinary (13):
Since 13 > 1, the function makes a recursive call: printBinary (13 / 2), which is printBinary (6).
Second Call to printBinary (6):
Since 6 > 1, the function makes another recursive call: printBinary (6 / 2), which is printBinary (3).
Third Call to printBinary (3):
Since 3 > 1, the function makes another recursive call: printBinary (3 / 2), which is printBinary (1).
Fourth Call to printBinary (1):
Since 1 <= 1, the function does not make a recursive call and proceeds to print 1 % 2, which is 1.
Returning from Third Call:
After printBinary (1) prints 1, the third call resumes and prints 3 % 2, which is 1.
Returning from Second Call:
After printBinary (3) prints 1, the second call resumes and prints 6 % 2, which is 0.
Returning from First Call:
After printBinary (6) prints 0, the first call resumes and prints 13 % 2, which is 1.
Output:
The binary representation printed is 1101.

2) I/p: I am Vinay Kumar and o/p: Kumar Vinay am I


#include<stdio.h>
void revword(char *st,char *en)
{
char t;
while(st<en)
{
t=*st;
*st=*en;
*en=t;
st++;
en--;
}
}
void revstr(char *s)
{
char *q,*t=s;
q=s;
while(*t)
{
t++;
if(*t=='\0')
revword(q,t-1);
if(*t==' ')
{
revword(q,t-1);
q=t+1;
}
}
}
char* strrev(char *s)
{
char *q,t;
q=s;
while(*q)
q++;
q--;
while(s<q)
{
t=*s;
*s=*q;
*q=t;
s++;
q--;
}
return s;
}

void main()
{
char s[50];
printf("enter the string...\n");
scanf("%[^\n]",s);
printf("orginal string:%s\n",s);
strrev(s);
printf("\n");
printf("Reversed string:%s\n",s);
revstr(s);
printf("\n");
printf("Reversed string by words:%s\n",s);
}
3) WAP to make our own strcpy, strchr,strrev,strcmp ?
#include<stdio.h>
void mystrcpy(const char* s,char *d)
{
while(*s)
{
*d=*s;
s++;
d++;
}
*d='\0';
}
int mystrchr(char *s,char ch)
{
for(int i=0;s[i];i++)
{
if(s[i]==ch)
return 1;
}
return 0;
}
void mystrrev(char* s)
{
char *d,t;
d=s;
while(*d)
d++;
d--;
while(s<d)
{
t=*s;
*s=*d;
*d=t;
s++;
d--;
}
}
int mystrcmp(char *s,char *d)
{
for(int i=0;s[i];i++)
{
if(s[i]!=d[i])
return 1;
}
return 0;
}
void main()
{
char s[10],d[10],ch;
printf("enter the string\n");
scanf("%s",s);
mystrcpy(s,d);
printf("copied: %s\n",s);
printf("***************************\n");
printf("enter the char to search\n");
scanf(" %c",&ch);
int r=mystrchr(s,ch);
if(r==1)
printf("char is present\n");
else
printf("char is not present\n");
printf("***************************\n");
mystrrev(s);
printf("Reversed string: %s\n",s);
printf("***************************\n");
printf("enter the 2 string\n");
scanf("%s %s",s,d);
int c=mystrcmp(s,d);
if(c==0)
printf("strings are equal\n");
else
printf("strings are not equal\n");
printf("***************************\n");
}

4) WAP to remove extra space in the given string?


#include<stdio.h>
#include<string.h>
void main()
{
char s[20];
int l,i,j;
printf("enter\n");
scanf("%[^\n]",s);
l=strlen(s);
for(i=0;i<l;i++)
{
if(s[0]==' ')
{
for(i=0;i<l-1;i++)
s[i]=s[i+1];
s[i]='\0';
l--;
i--;
}
if(s[i]==' ' && s[i+1]==' ')
{
for(j=i;j<l-1;j++)
s[j]=s[j+1];
s[j]='\0';
l--;
i--;
}
}
printf("%s",s);
}
5) WAP to count the words in the string using function?
#include<stdio.h>
int stringwords(char *);
int main(){
char s[100];
int c,i,w;
printf("enter the sente\n");
scanf("%[^\n]",s);
w=stringwords(s);
printf("total no of words %d: \n",w);
}
int stringwords(char *s)
{ int i,c=0;
for(i=0;s[i];i++)
{
if(s[i]==' ')
{
c++;
}
}
if(i>0)
c++;
return c;
}

6) WAP to count the even and odd string in the given string?
#include <stdio.h>
int main() {
char s[] = "i am vinay from vector 2023 batch v23be4v3";
int evenCount = 0;
int oddCount = 0;
// Iterate through each character in the string
for (int i = 0; s[i] != '\0'; i++) {
// Check if the character is a digit (0-9)
if (s[i] >= '0' && s[i] <= '9') {
// Convert the character to an integer
int digit = s[i] - '0';
// Check if the digit is even or odd
if (digit % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
}
}
printf("Even numbers count: %d\n", evenCount);
printf("Odd numbers count: %d\n", oddCount);
return 0;
}

7) WAP to do the palindrome in string?


#include<stdio.h>
#include<string.h>
int main() {
int i, l;
char s[20];
printf("Enter the string: ");
scanf("%s", s);
l = strlen(s);
for(i = 0; i < l / 2; i++) {
if(s[i] != s[l - 1 - i]) {
printf("It's not a palindrome\n");
return 0; // Exit the program if not a palindrome
}
}
printf("It's a palindrome\n");
return 0;
}

8) WAP to do the palindrome in string using recursion function?


#include <stdio.h>
#include <string.h>
int isPalindrome(char s[], int start, int end) {
if (start >= end)
return 1; // Base case: single character or empty string is a palindrome
if (s[start] != s[end])
return 0; // Not a palindrome
// Recurse on the substring between start and end
return isPalindrome(s, start + 1, end - 1);
}
int main() {
char s[20];
printf("Enter a string: ");
scanf("%s", s);
int length = strlen(s);
if (isPalindrome(s, 0, length - 1))
printf("It's a palindrome!\n");
else
printf("It's not a palindrome.\n");
return 0;
}

9) WAP to delete a particular letter in a string?


#include<stdio.h>
void main()
{
int i,j;
char s[50],ch;
printf("enter the string\n");
scanf("%s",s);
printf("enter the char\n");
scanf(" %c",&ch);
for(i=0;s[i];i++)
{
if(s[i]==ch)
{
for(j=i;s[j];j++)
s[j]=s[j+1];
i--;
}
}
printf("%s",s);
}

10) WAP to print the hexadecimal number without using %x?


#include <stdio.h>
int main()
{
int num, rem, i;
char hex_symbols[] = {0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F'};
printf("Enter a number: ");
scanf("%d", &num);
printf("0x");
while (num > 0)
{
rem = num % 16;
printf("%c", hex_symbols[rem]);
num = num / 16;
}
printf("\n");
}
11) WAP for strong number b/w 100 to 500
#include<stdio.h>
void main()
{
int n1,n2,i,j,fact,sum=0,p;
printf("enter the number\n");
scanf("%d %d",&n1,&n2);
for(i=n1;i<=n2;i++)
{
int temp=i;
//sum=0;
while(temp>0)
{
p=temp%10;
for(j=1,fact=1;j<=p;j++)
{
fact=fact*j;
}
sum=sum+fact;
temp=temp/10;
}
if(sum==i)
printf("%d its perfect number\n",i);
}
}
12) WAP to swap alternate bits for given number
#include<stdio.h>
void main()
{
int n,pos,p,q,r;
printf("enter the number\n");
scanf("%d",&n);
for(pos=15;pos>=0;pos--)
printf("%d",n>>pos&1);
printf("\n");
p=(n&0xAAAA)>>1;
q=(n&0x5555)<<1;
r=p|q;
for(pos=15;pos>=0;pos--)
printf("%d",r>>pos&1);
}

13) WAP to count clear bits in given number


#include<stdio.h>
void main()
{
int n,i,pos=32,c=0;
printf("enter the num\n");
scanf("%d",&n);
for(i=0;i<pos;i++)
{
if((n&(1<<i))==0)
c++;
}
printf("%d\n",c);
}
14) WAP to count set bits in given number
#include<stdio.h>
void main()
{
int n,i,pos=32,c=0;
printf("enter the num\n");
scanf("%d",&n);
for(i=0;i<pos;i++)
{
if((n&(1<<i)))
c++;
}
printf("%d\n",c);
}
15) Given number 35281 Need to sum even digits And mul odd digits?
#include<stdio.h>
void main()
{
int n,s,p,sum,prd,i;
printf("enter the number\n");
scanf("%d",&n);
sum=0;
prd=1;
for(i=n;i>0;i=i/10)
{
s=i%10;
if(s%2==0)
sum=sum+s;
else
prd=prd*s;
}
printf("%d %d",sum,prd);
}
16) i) - 48>>2 ii) 5)-412>>3
11 0000
00 1111
+ 1
---------
01 0000  -48
---------
00 0100  after shifting 2

Again, do the 2s compliment for the above binary u will get -12.
17) WAP for palindrome?
#include<stdio.h>
void main()
{
int i,j,n1=100,n2=200,s;
for(i=n1;i<n2;i++)
{
for(j=i,s=0;j;j=j/10)
{
s=s*10+j%10;
}
if(s==i)
printf("%d\n",i);
}
}
18) WAP to delete particular nibble?
#include<stdio.h>
void main()
{
int num,n,pos;
printf("enter the number\n");
scanf("%d",&num);
printf("enter the nibble number\n");
scanf("%d",&n);
for(pos=31;pos>0;pos--)
printf("%d",(num>>pos)&1);
printf("\n");
if(n<0 || n>7)
{
return;
}
num=num&(~(0xF<<(n*4)));
for(pos=31;pos>0;pos--)
printf("%d",(num>>pos)&1);
}
19) WAP to revrese only 3 letters words in a string?
#include<stdio.h>
void wordrev(char *s)
{
char t;
t=s[0];
s[0]=s[2];
s[2]=t;
}
void rev(char *start)
{
char *word=start;
int l=0;
while(*start)
{
if(*start==' '|| *(start+1)=='\0')
{
l=start-word;
if(l==3)
{
wordrev(word);
}
word=start+1;
}
start++;
}
}
void main()
{
char s[20];
printf("enter the str\n");
scanf("%[^\n]",s);
printf("original str: %s\n",s);
rev(s);
printf("modified str: %s\n",s);
}
20) Pattern
1357
357
57
7
#include<stdio.h>
void main()
{
int i,j,k;
for(i=1;i<=4;i++)
{
int n=1+(i-1)*2;
for(k=4;k>4-i;k--)
printf(" ");
for(j=4;j>=i;j--)
{
printf("%d",n);
n=n+2;
}
printf("\n");
}
}

ii) E
12
CDE
1234
ABCDE
#include<stdio.h>
void main()
{
int i,j,k,n;
for(i=1;i<=5;i++)
{
char ch='A';
for(k=1;k<=4-i;k++)
printf(" ");
if(i%2==0)
{
for(j=1,n=1;j<=i;j++)
{
printf("%d",n++);
}
}
else
{
for(j=1;j<=i;j++)
{
printf("%c",ch+(5-I ));
ch++;
}
}
printf("\n");
}
}

*
A*
B C*
D E F*

#include<stdio.h>
void main()
{
int i,j,k,n;
char ch='A';
for(i=1;i<=4;i++)
{
for(k=1;k<=4-i;k++)
printf(" ");
for(j=1;j<i;j++)
{
printf("%c ",ch);
ch++;
}
printf("*\n");
}
}
21) Delete the largest digit from given number e.g. 1534 O/p: 134
#include<stdio.h>
#include<string.h>
void del(char* s)
{
int len=strlen(s);
char lar=s[0];
int pos;
for(int i=0;i<len;i++)
{
if(s[i]>lar)
{
lar=s[i];
pos=i;
}
}
for(int i=pos;i<len-1;i++)
s[i]=s[i+1];
s[len-1]='\0';
printf("deleted largest num:%s\n",s);
}
void main()
{
char num[10],s;
printf("enter the num\n");
scanf("%s",num);
del(num);
}
22) WAP to print prime numbers using recursive function and normal?
#include <stdio.h>
void prime(int n1,int n2)
{
int i;
if(n1>n2)
return;
for(i=2;i<=n1;i++)
{
if(n1%i==0)
break;
}
if(i==n1)
{
printf("%d ",i);
prime(n1+1,n2);
}
else
prime(n1+1,n2);
}
int main()
{
int n1,n2,i,j;
printf("enter the number\n");
scanf("%d %d",&n1,&n2);
prime(n1,n2);
/* for(i=n1;i<=n2;i++)
{
for(j=2;j<=i;j++)
{
if(i%j==0)
break;
}
if(i==j)
printf("%d",i)
}*/
}
23) WAP to print palindrome numbers using recursive function and normal?
#include<stdio.h>
void palin(int n1,int n2)
{
int i,j,s;
if(n1>=n2)
return;
for(j=n1,s=0;j;j=j/10)
{
s=s*10+j%10;
}
if(n1==s)
{
printf("%d ",s);
palin(n1+1,n2);
}
else
palin(n1+1,n2);
}
void main()
{
int i,j,n1,n2,s;
printf("enter the numbers\n");
scanf("%d %d",&n1,&n2);
for(i=n1;i<=n2;i++)
{
for(j=i,s=0;j;j=j/10)
{
s=s*10+j%10;
}
if(i==s)
printf("%d ",i);
}
printf("\n");
palin(n1,n2);
}
24) WAP to print Armstrong numbers using recursive function and normal?
#include<stdio.h>
void arm(int n1,int n2)
{
int i,j,s,p,pc,c,n;
if(n1>n2)
return;
i=n1;
n=i;
c=0;
while(n)
{
c++;
n=n/10;
}
n=i;
s=0;
while(n)
{
p=1;
j=n%10;
for(pc=0;pc<c;pc++)
p=p*j;
s=s+p;
n=n/10;
}
if(s==i)
{
printf("%d\n",i);
arm(n1+1,n2);
}
else
arm(n1+1,n2);
}
void main()
{
int n1,n2,i,j,s,p,pc,c,n;
printf("enter\n");
scanf("%d %d",&n1,&n2);
arm(n1,n2);
}
25) WAP to print Fibonacci series numbers using recursive function and normal?
#include<stdio.h>
void fab(int a,int b,int n2)
{
int c;
c=a+b;
if(c>n2)
return;
printf("%d ",c);
fab(b,c,n2);
}
void main()
{
int i,j,s,n2;
printf("enter num\n");
scanf("%d",&n2);
for(i=0,j=1,s=1;s<n2;i=j,j=s)
{
s=s+i;
if(s<n2)
printf("%d ",s);
}
printf("\n");
fab(0,1,n2);
}
26) WAP to print Factorial numbers using recursive function?
#include<stdio.h>
void fact(int n,int i)
{
if(i>n)
return;
if(n%i==0)
printf("%d ",i);
fact(n,i+1);
}
void main()
{
int i,j,s,n2,n1;
printf("enter num\n");
scanf("%d %d",&n1,&n2);
for(i=n1;i<=n2;i++)
{
fact(i,1);
printf("\n");
}
}
27) WAP to print binary using char pointer?
28) 2)check given no. is prime or not using goto
29) 4) binary printing of double using short int pointer
30) [Link] for perfect number between 100 to 250
31) [Link] for count the set bits using bit wise
32) [Link] for reverse the bits
33) [Link] prediction on pointers
34) 7. Tell me the Complition stages
35) [Link] between continue & return
36) [Link] is pointer

You might also like