In: Computer Science
How do I write a C# and a C++ code for creating a character array containing the characters 'p', 'i', 'n','e','P','I','N','E' only and then using these lower and capital case letter character generate all possible combinations like PInE or PinE or PIne or PINE or piNE etc. and only in this order so if this order is created eg. NeIP or EnPi or NeIP or IPnE and on. You can generate all the combinations randomly by creating the word pine in any combination of lowercase or uppercase letters.Such that if the user entered PiNe or in this order, the compiler would recognize this order and print out the PiNe entered by the user.
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
// function to generate all possible combinations of given char array
void generate(char arr[],string res[],char temp[],int low,int high,int ind,int size)
{
if(ind==size)
{
string s="";
for (int i=0;i<size;i++)
{
s+=temp[i];
}
int i=0;
while(res[i]!=""){// move till empty location
i++;
}
res[i]=s;// store word formed in string array res
return;
}
for(int i=low;i<=high&&high-i+1>=size-ind;i++)
{
temp[ind]=arr[i];
generate(arr,res,temp,i+1,high,ind+1,size);
}
}
// driver code
int main()
{
char array[] = {'p', 'i', 'n', 'e', 'P','I','N','E'}; // char array
string res[5000];
char temp[4];
int n=sizeof(array)/sizeof(array[0]);
generate(array,res,temp,0,n-1,0,4);
int i=0;
cout<<"Enter word: ";
string word;
cin>>word;
string w=word;
transform(word.begin(),word.end(),word.begin(), ::tolower);// convert string to lower case
while(res[i]!=""){
if(res[i]==word){// word entered by used found in generated words
cout<<w<<" entered by the user"<<endl;
return 0;
}
i++;
}
cout<<"Cannot recongnized by compiler";// word not found
return 0;
}
//////////////C# code.........Another easy way is to convert entered word into lower case and then check if it equals to "pine"
using System;
class HelloWorld {
static void Main() {
char[] arr={'p','i','n','e','P','I','N','E'};
string check="pine";
string word;
Console.Write("Enter word: ");
word=Console.ReadLine();
if(check.Equals(word.ToLower()))
Console.Write("Entered by the user");
else
Console.Write("Cannot recongnized by the compiler");
}
}