expand(int s[]) 3-3 knr

This is a discussion on expand(int s[]) 3-3 knr within the C Programming forums, part of the General Programming Boards category; I'm having trouble with the code below. Basically copying s[i] into a int temp_one variable if it meets the criteria ...

  1. #1
    Insomniac
    Join Date
    Mar 2004
    Posts
    35

    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);
      	}
    Last edited by olbas; 04-06-2004 at 12:14 PM.
    i386 FreeBSD -- gcc-3.2.1 -- gdb-5.3

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    524
    well first of all, both printf's are trying to print strings, but you're passing them ints.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Last edited by quzah; 04-06-2004 at 12:05 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise setbits function (knr 2-6)
    By olbas in forum C Programming
    Replies: 17
    Last Post: 03-10-2010, 07:40 AM
  2. KnR, Ch1, ex 1-10
    By Layla Nahar in forum C Programming
    Replies: 1
    Last Post: 08-20-2006, 11:52 AM
  3. Ex. 2-4 KnR (2nd ed.) loop && array handling
    By olbas in forum C Programming
    Replies: 2
    Last Post: 03-23-2004, 11:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21