Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! First of all thank you to all who replied to my earlier question.Moreover I want to say sorry to everyone whom i replied with rude comments that "I was not screaming" , actually I did not Know that words in capital means to scream.
Ques
Actually I wanted to print series like
aa
ab
ac
...
I am now able to print till end of combinations of first two letters using for loop.
Is there any way to move on to three combinations using for loop.

Thanks in advance and sorry for my previous rude replies.

What I have tried:

#include<iostream>

using namespace std;

int main()
{
	
	char a = 'a';
	string b[26];
	string c[7500];
	int e;
	int i;
	int f = 0;
	for(i=0;i<=25;i++)
	{
		b[i] = a++;
	}
while(1)
{
		 	 	
 for(int e=0;e<=26;e++)
	 {
	 	c[e] = b[f]+b[e];
	 	cout<<c[e]<<endl;
	     	if(e==26)
	     	{
	     		f++;
			 }
	 }
      if(f==26)
      {
      	break;
	  }
}
		getchar();
		return 0;
	
	
}
Posted
Updated 21-May-17 6:37am

1 solution

Why are you using numbers, when the language allows you to use letters just the same:
C++
for (char c1 = 'a'; c1 <= 'z'; ++c1)
{
    cout << c1 << endl;
}

So to print multiple character combinations, you just need to add new loops inside the first one, thus:
C++
for (char c1 = 'a'; c1 <= 'z'; ++c1)
{
    for (char c2 = 'a'; c2 <= 'z'; ++c2)
    {
        for (char c3 = 'a'; c3 <= 'z'; ++c3)
        {
            cout << c1 << c2 << c3 << endl;
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900