This repository has been archived by the owner on Sep 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrcentral.c
165 lines (132 loc) · 4.18 KB
/
strcentral.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* String Central
* CS 480 - Assignment 2
*
* By Andrew Stebenne
* 4 February, 2015
*
*
* String Central is a function which processes input strings according to a
* selected function from an array of [pointers to] functions which modify
* strings.
*
* Here, giving the main function an input string will cause it to print out
* the result of all the available transformations on that string.
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
char* char_double(char* str);
char* char_shift(char* str);
char* char_flip(char* str);
char* char_double(char* str)
{
int length = strlen(str);
// Output should be twice the length of the input
char* output = malloc( sizeof(char) * length * 2 );
char* str_iterator = str;
char* out_iterator = output;
// Copy the i'th char into the i'th and i+1'th spots
while( *str_iterator )
{
// Copy into the i'th position
*out_iterator = *str_iterator;
out_iterator++;
// Copy into the i+1'th position
*out_iterator = *str_iterator;
out_iterator++;
// Now move the pointer to str forward
str_iterator++;
}
return output;
}
char* char_shift(char* str)
{ // For each letter at position i in the input string, the output string
// should have the same letter at position 2*i, plus the next
// letter at position (2*i) + 1
int length = strlen(str);
// Output should be twice the length of the input
char* output = malloc( sizeof(char) * length * 2 );
char* str_iterator = str;
char* out_iterator = output;
char temp;
while( *str_iterator )
{
// Copy the i'th char into the i'th spot
*out_iterator = *str_iterator;
out_iterator++;
// Copy (str[i] + 1) into position i+1
temp = *str_iterator + 1;
*out_iterator = temp;
out_iterator++;
str_iterator++;
}
return output;
}
char* char_flip(char* str)
{ // Should reverse upper and lower cases in the input string
int length = strlen(str);
// Output should be twice the length of the input
char* output = malloc( sizeof(char) * length * 2 );
char* str_iterator = str;
char* out_iterator = output;
while( *str_iterator )
{
if( islower( *str_iterator ) )
{
*out_iterator = toupper(*str_iterator);
}
else if( isupper( *str_iterator ) )
{
*out_iterator = tolower(*str_iterator);
}
else
{ // If it's not a letter, just leave it alone.
*out_iterator = *str_iterator;
}
out_iterator++;
str_iterator++;
}
return output;
}
char** string_central(
char* (*functions[])(char*),
char* input_string )
{
// Pointer into the function array
char* (*f_iterator)(char*) = *functions;
// Length of the input string
int length = strlen(input_string);
// out_array will hold three pointers to the strings we've modified.
char** out_array;
out_array = malloc( sizeof(char*) * 4 );
out_array[0] = malloc( sizeof(char) * length * 2 );
out_array[1] = malloc( sizeof(char) * length * 2 );
out_array[2] = malloc( sizeof(char) * length );
out_array[3] = malloc( sizeof(char) );
// Copy from the output of (the function that we are currently pointing
// at) into (the corresponding space in out_array), iterate and do the
// same again.
strcpy( out_array[0], functions[0](input_string) );
strcpy( out_array[1], functions[1](input_string) );
strcpy( out_array[2], functions[2](input_string) );
strcpy( out_array[3], "\0" );
return out_array;
}
int main( int argc, char* argv[] )
{
char* (*first)(char*) = char_double;
char* (*second)(char*) = char_shift;
char* (*third)(char*) = char_flip;
char* ( *processors[] )( char* ) = {first, second, third, NULL};
// out_array will hold pointers to the strings we processed
char** out_array = string_central( processors, argv[1] );
// Iterator to read from out_array
char** read_out = out_array;
while( *read_out )
{
printf( "%s\n", *read_out );
read_out++;
}
return 0;
}