Thread: possible eight-letter combinations

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    7

    possible eight-letter combinations

    // this code should generate all possible eight-letter combinations from an eight-digit phone number. The procedure doesn't check if the phone number contains 0's or 1's.

    Code:
    #include<iostream>
    using namespace std;
    
    const char* letters[] = {
        "ABC",
        "DEF",
        "GHI",
        "JKL",
        "MNO",
        "PRS",
        "TUV",
        "WXY"
    };
    FILE* fp;
    char str1[15];
    
    void GenCombinations (
        const char* number,
        char str[]) 
    {
        if (*number == '\0')
        {
    	fwrite(str - 8, 1, 8, fp);
    	cout<<str<<endl;
    	fwrite("\r\n", 1, 2, fp);
    	return;
        }
        else if (*number == '-')
    	GenCombinations(number + 1, str);
        else {
    	const int group = *number - '0' - 2;
    	for (int i = 0; i < 3; i++)
    	{
    	    *str = letters[group][i];
    	    cout<<str<<endl;
    	    GenCombinations(number + 1, str + 1);
    	}
        }
    }
    int main(){
        char str2[]="42472888";
        strcat("\0",str2);
        GenCombinations(str2,str1);
         cout<<str1<<endl;
           system("PAUSE");
        return 0;
        }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. counting letter occurences in a string
    By pjr5043 in forum C++ Programming
    Replies: 35
    Last Post: 05-05-2008, 09:18 PM
  2. Advice requested, Code makes sense to me, not compiler
    By andrew.bolster in forum C Programming
    Replies: 53
    Last Post: 01-06-2008, 01:44 PM
  3. help using strings and mapping
    By trprince in forum C Programming
    Replies: 29
    Last Post: 12-01-2007, 04:01 PM
  4. Big Letter became small letter
    By cogeek in forum C Programming
    Replies: 27
    Last Post: 12-13-2004, 02:04 PM
  5. Combinations
    By GaPe in forum C Programming
    Replies: 16
    Last Post: 01-09-2002, 05:38 AM