top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

c++ programm for all permutation of elements

+2 votes
774 views

Write a C/C++ program to print all possible strings of length k that can be formed from a set of n characters?

posted Aug 5, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

try this:

#include<stdio.h>
#include<string.h>
void fun(int j);
int k;
int len;
char str[10],str1[20];
main()
{
    printf("enter alpha\n");
    scanf("%s",str);
    for(;str[len]!='\0';len++);
    printf("enter k\n");
    scanf("%d",&k);
    str1[k]='\0';
    fun(0); 
}
void fun(int j)
{
    if(j>=k) 
    {
        for(j=0;j<k;j++)
            printf("%c",str1[j]);
        printf(" ");
        return;
    }   
    int i;
    for(i=0;i<len;i++)
    {   
        str1[j]=str[i];
        fun(j+1);
    }
}
answer Aug 6, 2016 by Mahedra Chaudhari
This code is not working, can your recheck.
Means getting an error?
Not getting desired result, please recheck.
0 votes

Following is the tested code, let me know if this does not work.

Approach: Start from an empty string and add characters one by one to it using a recursive function and printing it.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void print_string(char str[],char new_str[],int current_len,int n,int len)
{
    /*
    str=orignal set,
    new_str=empty char array,
    current_len=0(Intially)
    n=no of elements to be used
    len=the value of p given
    */
    if(current_len==len)//print string when length is equal to p
    {
        printf("%s\n",new_str);
        return;
    }
    else
    {
        int i;
        for(i=0;i<n;i++)
        {
            new_str[current_len]=str[i];
            print_string(str,new_str,current_len+1,n,len);
        }
    }
}
int main()
{
    char set[]="QueryHome";
    char arr[100]="";
    print_string(set,arr,0,9,3);
    return 0;
}
answer Aug 7, 2016 by Salil Agrawal
Similar Questions
+1 vote

Say I want to write a function void removeAll(linkedListType& obj);

NOTE: this function is NOT a member function of the linkedListType class. .

#include <iostream>
using namespace std;

struct nodeType
{
    int info;
    nodeType *next;
};

class linkedListType
{
public:
    linkedListType();
    int listSize();
    bool isEmpty();
    int seqSearch(int);
    void remove(int);
    void insertFirst(int);
    void insertEnd(int);
    void insertAt(int, int);
    void removeAt(int);
    void print();
    void clearList();
    void insertOrdered(int);
    void removeFirst();
    void removeLast();
    void removeLast2();
    int removeOddSumEven();
     ~linkedListType();  


private:
    nodeType *first, *last;
    int length;
};

// This function assumes that the list is NOT empty
// and first and last elements are NOT odd
int linkedListType::removeOddSumEven()
{
    int sum = first->info;
    nodeType *current = first->next;
    nodeType *trailCurrent = first;

    while(current != NULL)
    {
        if(current->info % 2 == 0)
        {
            sum += current->info;
            trailCurrent = current;
            current = current->next;
        }
        else
        {
            trailCurrent->next = current->next;
            delete current;
            length--;
            current = trailCurrent->next;
        }
    }
    return sum;
}



void linkedListType::removeLast()
{
    if(length == 0)
        cout<<"ERROR: EMPTY LIST" << endl;
    else if(length == 1)
    {
            delete first;
            last = first = NULL;
            length--;
    }
    else
    {
        nodeType *current = first->next;
        nodeType *trailCurrent = first;
        while(current != last)
        {
            trailCurrent = current;
            current = current->next;
        }
        delete current;
        trailCurrent->next = NULL;
        last = trailCurrent;
        length--;
    }
}

void linkedListType::removeLast2()
{
    if(length == 0)
        cout<<"ERROR: EMPTY LIST" << endl;
    else if(length == 1)
    {
            delete first;
            last = first = NULL;
            length--;
    }
    else
    {
        nodeType *current = first;
        while(current->next != last)
            current = current->next;

        delete last;
        current->next = NULL;
        last = current;
        length--;
    }
}

void linkedListType::removeFirst()
{
    if(length == 0)
        cout<<"ERROR: EMPTY LIST" << endl;
    else if(length == 1)
    {
            delete first;
            last = first = NULL;
            length--;
    }
    else
    {
        nodeType *current = first;
        first = first->next;
        delete current;
        length--;
    }
}

linkedListType::linkedListType()
{
    first = last = NULL;
    length = 0;
}

int linkedListType::listSize()
{
    return length;
}

bool linkedListType::isEmpty()
{
    return (length == 0);
}

int linkedListType::seqSearch(int item)
{
    nodeType *current = first;
    int loc = 0;
    while(current != NULL)
    {
        if(current->info == item)
            return loc;
        current = current->next;
        loc++;
    }
    return -1;
}

void linkedListType::remove(int item)
{
    if(isEmpty())
    {
        cout<<"Can not remove from empty list\n";
        return;
    }

    nodeType *current, *trailCurrent;
    if(first->info == item)//delete the first element, special case
    {
        current = first;
        first = first->next;
        delete current;
        length--;
        if(length == 0)
            last = NULL;
    }
    else
    {
        current = first->next;
        trailCurrent = first;
        while(current != NULL)
        {
            if(current->info == item)
                break;
            trailCurrent = current;
            current = current->next;
        }

        if(current == NULL)
            cout<<"The item is not there\n";
        else
        {
            trailCurrent->next = current->next;
            if(last == current) //delete the last item
                last = trailCurrent;
            delete current;
            length--;
        }
    }
}

void linkedListType::insertFirst(int item)
{
    nodeType *newNode = new nodeType;
    newNode->info = item;
    if(length == 0){
        first = last = newNode;
        newNode->next = NULL;
    }
    else{
        newNode->next = first;
        first = newNode;
    }
    length++;
}

void linkedListType::insertEnd(int item)
{
    nodeType *newNode = new nodeType;
    newNode->info = item;
    if(length == 0){
        first = last = newNode;
        newNode->next = NULL;
    }
    else{
        last->next = newNode;
        newNode->next = NULL;
        last = newNode;
    }
    length++;
}

void linkedListType::insertAt(int loc, int item)
{
    if(loc < 0 || loc > length)
        cout<< "ERROR: Out of range" << endl;
    else
    {
        nodeType *newNode = new nodeType;
        newNode->info = item;
        if (loc == 0) //insert at the begining
            insertFirst(item);
        else if (loc == length) //insert at the end;
            insertEnd(item);
        else
        {
            nodeType *current = first;
            for(int i = 1; i < loc; i++)
                current = current->next;
            newNode->next = current->next;
            current->next = newNode;    
            length++;
        }

    }
}

void linkedListType::removeAt(int loc)
{
    if(loc < 0 || loc >= length)
        cout<< "ERROR: Out of range" << endl;
    else
    {
        nodeType *current, *trailCurrent;
        if(loc == 0) //remove the first item
        {
            current = first;
            first = first->next;
            delete current;
            length--;
            if(length == 0)
                last = NULL;
        }
        else
        {
            current = first->next;
            trailCurrent = first;
            for(int i = 1; i < loc; i++)
            {
                trailCurrent = current;
                current = current->next;
            }

            trailCurrent->next = current->next;
            if(last == current) //delete the last item
                last = trailCurrent;
            delete current;
            length--;
        }
    }
}    

void linkedListType::print()
{
    nodeType *current = first;
    while(current != NULL)
    {
        cout<<current->info <<endl;
        current= current->next;
    }
}

void linkedListType::clearList()
{
    nodeType *current;
    while(first != NULL)
    {
        current = first;
        first = first->next;
        delete current;
    }
    last = NULL;
    length = 0;
}

void linkedListType::insertOrdered(int item)
{
    nodeType *newNode = new nodeType;
    newNode->info = item;

    if(first == NULL)
    {
        first = last = newNode;
        newNode->next = NULL;
        length++;
    }
    else if(first->info >= item)
    {
        newNode->next = first;
        first = newNode;
        length++;
    }
    else
    {
        nodeType *current = first->next;
        nodeType *trailCurrent = first;

        while(current != NULL)
        {
            if(current->info >= item)
                break;      
            current = current->next;
            trailCurrent = trailCurrent->next;
        }
        if(current == NULL) //insert at the end
        {
            last->next = newNode;
            newNode->next = NULL;
            last = newNode;
            length++;
        }
        else
        {
            trailCurrent->next = newNode;
            newNode->next = current;
            length++;
        }
    }
}
linkedListType::~linkedListType()
{
    clearList();
}

int main()
{
    linkedListType l1;

    l1.insertAt(0, 10);
    l1.insertAt(1, 1);
    l1.insertAt(2, 3);
    l1.insertAt(3, 40);

    l1.print();

    return 0;
}
0 votes

I want to get all the map values in c++? Please share sample code?

...