Help with code for encoding and decoding letters

This is a discussion on Help with code for encoding and decoding letters within the C Programming forums, part of the General Programming Boards category; Hi I'm pretty new to programming and I need some help getting this code to work. Its supposed to offer ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    13

    Help with code for encoding and decoding letters

    Hi I'm pretty new to programming and I need some help getting this code to work. Its supposed to offer the user a choice to either encode letters or decode them. Their supposed to enter 1 to encode, where they enter a number of random letters "khgdhf" and the result will be "1k2h1g1d1f" or 2 to decode where they enter the opposite "2h4g1t" and it displays "hhggggt". After I enter the choice nothing happens? Any help would be appreciated.

    Code:
    # include <stdio.h>
    # include <ctype.h>
    void		encode(void);
    void		decode(void);
    
    int main(void)
    {
    	int choice;
    
    	printf("Type 1 to encode, 2 to decode:");
    	scanf("%d", &choice);
    
    	if (choice==1)
    	
    		encode();
    	
    	if (choice==2)
    	
    		decode();
    	
    
    }
    void encode(void)
    
    {
    	char ch, nextch;
    	int count;
    
    	do
    	{
    		scanf("%c", &ch);
    	}
    	while (ch!='*');
    	printf("%c",ch);
    	scanf("%c", &ch);
    	do
    	{
    		count=1;
    		nextch=ch;
    		while (nextch==ch)
    		{
    			count=count+1;
    			scanf("%c", &nextch);
    		}
    		if (count>1)
    		{
    			printf("%d%c", count, ch);
    		}
    		else
    		{
    	printf("%c", ch);
    }
    ch=nextch;
    }
    while (ch!='*');
    printf("%c",ch);
    printf("\n\n");
    }
    
    void decode(void)
    {
    	char ch;
    	int count, i;
    
    	do
    	{
    		scanf("%c", &ch);
    	}
    	while(ch!='*');
    	printf("%c",ch);
    
    	scanf("%c", &ch);
    	do
    	{
    		if (isdigit(ch))
    		{
    			count=ch-'0';
    			scanf("%c", &ch);
    			for(i=0;i<count;++i)
    				printf("%c",ch);
    		}
    		else
    		{
    			printf("%c",ch);
    		}
    		scanf("%c", &ch);
    	}
    	while(ch!='*');
    	printf("%c",ch);
    	printf("\n\n");
    }

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    Nevermind I got it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. decoding code
    By C++Newb in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2009, 12:00 AM
  2. Code decoding
    By ash4741 in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2009, 10:57 AM
  3. Help with decoding program...
    By porsche911nfs in forum C++ Programming
    Replies: 18
    Last Post: 04-10-2009, 01:21 AM
  4. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  5. can somone please help me with this code?
    By amsy in forum C Programming
    Replies: 1
    Last Post: 10-25-2004, 01:23 PM

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