-
expand(int s[]) 3-3 knr
I'm having trouble with the code below. Basically copying s[i] into a int temp_one variable if it meets the criteria set in the loops found in expand. I was hoping that I could rely on the ASCII codes to generate the expanded string, but both print statements in main are printing null.
With the nulls in mind, I assume the loop which assigns the values to the elements in t are incorrect. But it seems ok... lol maybe I'm blind. Anyway, any help?
Code:
#include <stdio.h>
#include <ctype.h>
#define IN 0
#define OUT 1
#define PASS 2
#define READY 3
#define MAX 100
#define MAX2 300
int expand(int s[]);
int t[MAX2] = { 0 };
int main(void) {
int i, c = 0;
int s[MAX] = { 0 };
extern int t[MAX2];
printf("%s", "please enter a string\n");
for (i = 0; (c = getchar()) != EOF && c < MAX && c != '\n'; i++)
s[i] = c;
printf("%s\n", expand(s));
printf("%s\n", *t);
return 0;
}
int expand(int s[]) {
int i = 0, j = 0;
int temp_one = 0;
int temp_two = 0;
short state = OUT;
extern int t[MAX2];
for (i = 0; i < MAX; i++) {
if (state == OUT) {
if (s[i] == '-')
state = PASS;
else if (s[i] == ' ')
state = OUT;
else if (isalnum(s[i]) != '0') {
state = PASS;
temp_one = s[i];
}
}
else if (state == PASS) {
if (s[i] == ' ')
state = OUT;
else if (s[i] == '-')
state == IN;
else
state = OUT;
}
else if (state == IN) {
if (s[i] == '-')
state = OUT;
else if (s[i] == ' ')
state = OUT;
else if (isalnum(s[i]) != '0') {
temp_two = s[i];
state = READY;
}
}
else if (state == READY) {
j = 0;
t[j] = temp_one;
for (j = 1; j < (temp_two - temp_one); j++) {
temp_one += 1;
t[j] = temp_one;
}
state = OUT;
}
}
return (*t);
}
-
well first of all, both printf's are trying to print strings, but you're passing them ints.
-
Here's an idea... Stop making expand return an integer if you're trying to print a string.
[edit]
Oh what the hell... Here's a bit more...
1) Use [code] tags, not PHP.
2) extern does not do what you think it's doing.
[/edit]
Quzah.