forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASCII_Subsequences.c
110 lines (92 loc) · 2.38 KB
/
ASCII_Subsequences.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*ASCII SUBSEQUENCES PROBLEM
This problem is same as the "Subsequences" question just instead of two function calls, three function calls will be made :
- One with the same string.
- One with a letter added.
- One with the ASCII code of the letter added to the string.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
char str[100];
void subseq(char s[], int i, char str[])
{
if(i == strlen(str))
{
printf("%s\n", s);
return;
}
// The 3 function calls in the same order as explained above.
// First function call
subseq(s, i + 1, str);
int i1;
char s2[strlen(s) + 2];
for(i1 = 0; i1 < strlen(s); i1++)
{
// Copy string from s to s2
s2[i1] = s[i1];
}
// Concatinate ith character of string str to s2
s2[i1++] = str[i];
s2[i1] = '\0';
// Second function call
subseq(s2, i + 1, str);
// Integer n stores the ASCII value of the ith character of string str
int i2, n = (int)str[i];
char s3[strlen(s) + 2];
// Create AsciiString to store the ASCII value of the ith character of string str
char AsciiString[5];
if(n < 10)
{
AsciiString[0] = (char)(n + 48);
AsciiString[1] = '\0';
}
else if(n<100)
{
AsciiString[0] = (char)(((n - (n % 10)) / 10) + 48);
AsciiString[1] = (char)((n % 10) + 48);
AsciiString[2] = '\0';
}
else
{
AsciiString[0] = (char)(((n - (n % 100)) / 100) + 48);
AsciiString[1] = (char)((((n - (n % 10)) / 10) % 10) + 48);
AsciiString[2] = (char)((n % 10) + 48);
AsciiString[3] = '\0';
}
for(i2 = 0; i2 < strlen(s); i2++)
{
// Copy string from s to s3
s3[i2] = s[i2];
}
int i3 = i2;
for(; i2 < strlen(AsciiString) + strlen(s); i2++)
{
// Concatinate AsciiString into s3
s3[i2] = AsciiString[i2 - i3];
}
s3[i2] = '\0';
// Third function call
subseq(s3, i + 1, str);
}
int main()
{
printf("Enter the string\n");
scanf("%s", str);
printf("Number of Subsequences : %d\n", (int)pow(3, strlen(str)));
subseq("", 0, str);
return 0;
}
/*Sample Input: Enter the string:
ab
Sample Output:
Number of Subsequences : 9
b
98
a
ab
a98
97
97b
9798
*/