I usually program in java but I'm going to implement this in C but the general logic psuedo code should work I would think.

these numbers represent these characters:

2-abc
3-def
4-ghi
5-jkl
6-mno
7pqrs
8-tuv
9-wxyz

If I were to type in a 7 digit number say 233-7687 as an input, I need to print out all the possible combinations of characters that number represents.

Ex: 233-7687 one combination could be "add soup" among other random combinations like "bde pmur" ect.

I found a function in C that works that can print out all the combinations of a string

comb("abc") would print "abc, acb, bac, bca, cab, cba"

I just can't figure out how to get different strings of the numbers given since each number can represent 3 different characters and 7 and 9 can represent 4 ect.

So I first attempted to do this in java. With some help I got a rough version working in Eclipse that's good enough for what I'm trying to do. However I spent the last day trying to translate this thing to C using the gcc compiler and I'm not having any luck. I'm pretty new to C and I'm struggling on this one.

Here's the working java version.

public static String[] letters = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

	public static void main(String[] args) 
	{
	printCombinations( "2337687", "");
	}

	public static void printCombinations( String numbers, String combination)
	{

	if(numbers.length() > 1) 
	{


	int number = Integer.parseInt( numbers.charAt(0) + "");
	
	for(char letter : letters[number].toCharArray())
	{
		
	printCombinations( numbers.substring(1), combination + letter);
	}

	} 
	else if(numbers.length() == 1) 
	{


	int number = Integer.parseInt( numbers.charAt(0) + "");

	for( char letter : letters[number].toCharArray()) 
	{
	System.out.println( combination + letter);
	}
	
	}

Here's my attempt at a C translation

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void printCombinations( char numbers[], char combination[]);
int string_length(char str[]);

static char *letters[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

substr(char dest[], char src[], int offset, int len)
{
int i;
for(i = 0; i < len && src[offset + i] != '\0'; i++)
	dest[i] = src[i + offset];
dest[i] = '\0';
}

void append(char* s, char c)
{
int len = strlen(s);
s[len] = c;
s[len+1] = '\0';
}
int string_length(char str[])
{
int i;
for(i=0; i<10; i++)
{
if(str[i]=='\0')
{
return(i);
}
}
}

void main(){
char *numbers = "2337687";
printCombinations( numbers, "");
}

void printCombinations( char numbers[], char combination[])
{
  
  

if(string_length(numbers) > 1)
{
//int number = atoi( numbers[0]);
int i=0;
for(i=0;i<string_length(numbers);i++)
{
char string[1];
substr(string,numbers, 0, 1);
printCombinations(string, append(combination , numbers[i]));
}
}
else if(string_length(numbers) == 1)
{
//int number = atoi( numbers[0]);
int j=0;
for(j=0;j<string_length(numbers);j++ )
{
printf( "%s", append(combination , numbers[j]));
}
}
}

The main thing I'm stumped up on is doing the equivalent .substring(1) operation in the java example above. Also I'm trying to figure out how to translate the for-each loops properly.

Right now my compiler spits out these errors

52: error: invalid use of void expression
61: error: invalid use of void expression

No matter how much I've switched things around even re-typing the whole thing different ways, I'd always narrow it down to the two errors above. After 12 hours, I've given up.

I'd appreciate if anyone can help me out with this or even offer a better alternative solution to what I'm trying to do.

The concept seems simple but it's been hell trying to do this in C :'(.

The error on line 52 is that you cannot use string as a name of a variable ... I think the keyword string is reserved. If you notice it got high lighted when you pasted the code here

Also why are you doing an operation inside the print function ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.