Skip to content

Commit 7fa5ce6

Browse files
Move variable declarations
So that they're closer to where they're used, instead of all being at the top of the function.
1 parent 031572f commit 7fa5ce6

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

check_whitespace.c

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,19 @@
77
* leaving any internal spaces alone.
88
*/
99
char* strip(char* str) {
10-
int size;
11-
int num_spaces;
12-
int first_non_space, last_non_space, i;
13-
char* result;
14-
15-
size = strlen(str);
10+
int size = strlen(str);
1611

1712
// This counts the number of leading and trailing spaces
1813
// so we can figure out how big the result array should be.
19-
num_spaces = 0;
20-
first_non_space = 0;
21-
while (first_non_space<size && str[first_non_space] == ' ') {
14+
int num_spaces = 0;
15+
int first_non_space = 0;
16+
while (first_non_space < size && str[first_non_space] == ' ') {
2217
++num_spaces;
2318
++first_non_space;
2419
}
2520

26-
last_non_space = size-1;
27-
while (last_non_space>=0 && str[last_non_space] == ' ') {
21+
int last_non_space = size-1;
22+
while (last_non_space >= 0 && str[last_non_space] == ' ') {
2823
++num_spaces;
2924
--last_non_space;
3025
}
@@ -38,9 +33,11 @@ char* strip(char* str) {
3833

3934
// Allocate a slot for all the "saved" characters
4035
// plus one extra for the null terminator.
41-
result = calloc(size-num_spaces+1, sizeof(char));
36+
char* result = calloc(size-num_spaces+1, sizeof(char));
37+
4238
// Copy in the "saved" characters.
43-
for (i=first_non_space; i<=last_non_space; ++i) {
39+
int i;
40+
for (i = first_non_space; i <= last_non_space; ++i) {
4441
result[i-first_non_space] = str[i];
4542
}
4643
// Place the null terminator at the end of the result string.
@@ -54,24 +51,20 @@ char* strip(char* str) {
5451
* no spaces at the front or the back of the string.
5552
*/
5653
int is_clean(char* str) {
57-
char* cleaned;
58-
int result;
59-
6054
// We check if it's clean by calling strip and seeing if the
6155
// result is the same as the original string.
62-
cleaned = strip(str);
56+
char* cleaned = strip(str);
6357

6458
// strcmp compares two strings, returning a negative value if
6559
// the first is less than the second (in alphabetical order),
6660
// 0 if they're equal, and a positive value if the first is
6761
// greater than the second.
68-
result = strcmp(str, cleaned);
62+
int result = strcmp(str, cleaned);
6963

7064
return result == 0;
7165
}
7266

7367
int main() {
74-
int i;
7568
int NUM_STRINGS = 7;
7669
// Makes an array of 7 string constants for testing.
7770
char* strings[] = { "Morris",
@@ -83,7 +76,7 @@ int main() {
8376
" silliness "
8477
};
8578

86-
for (i=0; i<NUM_STRINGS; ++i) {
79+
for (int i = 0; i < NUM_STRINGS; ++i) {
8780
if (is_clean(strings[i])) {
8881
printf("The string '%s' is clean.\n", strings[i]);
8982
} else {

0 commit comments

Comments
 (0)