Character Array - almost? works

This is a discussion on Character Array - almost? works within the C Programming forums, part of the General Programming Boards category; trying to creat a program that reads characters one at a time. An array is created to hold Unique characters. ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    7

    Character Array - almost? works

    trying to creat a program that reads characters one at a time. An array is created to hold Unique characters. If a character already has been entered into the array, it is not entered again. My problem with this is in the for loop. The way i have the condition set up is so that is checks the partially filled array but also checks the next element which is never filled.

    Anyway, Here's what its supposed to do:

    Enter up to 20 unique characters:

    The authors are Jeri Hanly and Elliot D. Koffman

    output: TheautorsJiHnlydED.K

    I have two files - a driver and a function to be called - here is the code:

    MAIN function

    Code:
    #define SIZE 20
    
    void add_char(char A[], char ch, int *count);
    
    main()
    {
    	char character;
    	char ary[SIZE];
    	int count;
    	int i;
    
    	printf("Enter up to 20 unique characters; "
    		"quit with 2 control-Ds:\n");
    
            scanf(" %c", &character);
    
    	count = 0;
    
    	while(count < SIZE)
    	{
    		add_char(ary, character, &count);
                    scanf(" %c", &character); 
    	}
    
    	printf("\n");
            
    	for (i = 0; i < count; ++i)
    	{
    		printf("%c", ary[i]);
    	}
    	printf("\n\n");
    }
    ADD_CHAR function

    Code:
    void add_char(char A[], char ch, int *count)
    {
    	int i, add;
    	
    	add = 0;
    
    	printf("test for char: %c \n", ch);
    
    	for(i = 0; i <= *count; ++i)
    	{
    		if (A[i] == ch)
    		{
    		}
    		else
    		{
    			add = 1;
    		}
    	}
    
    	if (add == 1)
    	{
    		A[*count] = ch;
    		++(*count);
    	}
    }

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    I'm not sure, but, try something like that:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int exist (char *str, char c) {
        while (*str++)
            if (*str == c)
               return 1;
        
        return 0;
    }
    
    void add (char *str, char c,int *tmp) {
         int i;
         
         for (i=0; i<(*tmp); ++i)
            ;
         str[i] = c;
    }
    
    int main(void)
    {
        char str[100];
        char curr;
        int tmp = 0;
        
        while ((curr = getchar()) != '\n')
           if (!exist(str,curr)) {
              add(str,curr,&tmp);
              tmp++;
           }
        str[tmp] = '\0';
        
        printf("The result: %s\n",str);
        system("PAUSE");
        return 0;
    }
    Sorry about the code, he is messy, but it will give you an idea.
    good luck.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    thanks, but i'd really like to know how to use a for loop to test

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    A for loop to test exactly what? If a specified char is in your string?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting character array to integer array
    By quiet_forever in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2007, 05:48 AM
  2. Find '&' character in char array?
    By tidemann in forum C Programming
    Replies: 7
    Last Post: 10-19-2006, 05:04 AM
  3. Help with 2-D character array
    By choykawairicky in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2004, 12:12 AM
  4. Selection Sort on 2-D character array
    By TankCDR in forum C++ Programming
    Replies: 6
    Last Post: 07-20-2003, 11:59 AM
  5. Determine the size of a character array...
    By Nutshell in forum C Programming
    Replies: 1
    Last Post: 01-10-2002, 09:22 AM

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