Thread: Help with code for encoding and decoding letters

  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-28-2009, 11:00 PM
  2. Code decoding
    By ash4741 in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2009, 09:57 AM
  3. Help with decoding program...
    By porsche911nfs in forum C++ Programming
    Replies: 18
    Last Post: 04-10-2009, 12: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, 12:23 PM