Question

In: Computer Science

C programing. Ask user to enter a word on sting and print all possible combinations. (please...

C programing.
Ask user to enter a word on sting and print all possible combinations. (please don't use printer) Using recursion.
example
ask user to input
user: "ABC"
output:
ABC
ACB
BAC
BCA
CAB
CBA

Solutions

Expert Solution

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

void swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}

void permute(char *x, int l, int r)
{
int i;
if (l == r)
printf("%s\n", x);
else
{
for (i = l; i <= r; i++)
{
swap((x+l), (x+i));
permute(x, l+1, r);
swap((x+l), (x+i)); //backtrack
}
}
}

int main()
{
char str[20];
printf("Please enter a word: ");
scanf("%s",str);
int n = strlen(str);
permute(str, 0, n-1);
return 0;
}

* The order may be different but all the possible combinations will be printed.


Related Solutions

Write in C# please! Ask the user to enter all of their exam grades. Once they...
Write in C# please! Ask the user to enter all of their exam grades. Once they are done, calculate the minimum score, the maximum score and the average score for all of their scores. Perform this using at least 2 Loops (can be the same type of loop) and not any built in functions.
Create a C++ project called RobberyLanguage. Ask the user to enter a word. Write a function...
Create a C++ project called RobberyLanguage. Ask the user to enter a word. Write a function that will receive the word, use the word to compile a new word in Robbery language and return the new Robbery language word. In the function: Use a while loop and a newline character (‘\0’) to step through the characters in the original string. How to compile a word in Robbery language: Double each consonant and put the letter ‘o’ between the two consonants....
Create a console app c#. Using a List, ask the user to enter all of their...
Create a console app c#. Using a List, ask the user to enter all of their favorite things. Once they are done, randomly pick a value from the list and display it.
Write a program in Python to print all possible combinations of phone numbers. The length of...
Write a program in Python to print all possible combinations of phone numbers. The length of the number will be given. Also 3 digits will be given, which can not be used. No two consecutive digits can be same. A number containing 4 would always have 4 in the beginning.
In C# When the user enters an invalid value, ask the user to repeatedly enter the...
In C# When the user enters an invalid value, ask the user to repeatedly enter the value until a valid value has been entered. Gender must be ‘M’ or ‘F’. Residency must be ‘I’ or ‘O’. Existing Code: using System; public class Student {   public int credit;   public String firstname, lastname, gender, residency, edate;   public void input()   {     Console.WriteLine("\nWelcome to the Continental University Registration System!"); Console.WriteLine("\nEnter data about a student"); Console.Write("First Name: "); firstname = Console.ReadLine(); Console.Write("Last Name: "); lastname...
Ask the user to enter test scores. Once they have entered -1, print out the highest...
Ask the user to enter test scores. Once they have entered -1, print out the highest grade. Ensure that the grade entered is an integer.(Using Java Language) Enter integers between 0 and 100, -1 when finished. Enter a test score: [asdf] Enter an integer. Enter a test score: [32.5] Enter an integer. Enter a test score: [42] Enter a test score: [99] Enter a test score: [87] Enter a test score: [x] Enter an integer. Enter a test score: [-1]...
( USE C++ ) The program prompts the user to enter a word. The program then...
( USE C++ ) The program prompts the user to enter a word. The program then prints out the word with letters in backward order. For example, if the user enter "hello" then the program would print "olleh" show that it works .
Language C++ Ask the user to enter their weight (in pounds) and their height in inches....
Language C++ Ask the user to enter their weight (in pounds) and their height in inches. Calculate their Body Mass Index (BMI) and tell them whether they are underweight, overweight, or at optimal weight. BMI formula: weight * 703 / (height * height) Optimal weight is a BMI from 19 to 26. Lower is underweight, higher is overweight. Prompts: Enter your weight (in pounds): [possible user input: 144] Enter your height (in inches): [posible user input: 73] Possible Outputs: Your...
Using Bash script, 1. Print the multiplication table upto 10 rows. Ask the user to enter...
Using Bash script, 1. Print the multiplication table upto 10 rows. Ask the user to enter a number. say user enters 10, your output should be : [srivatss@athena shell]> ./mult.sh I will be printing the multiplication table Please enter a number 10 1 x 10 = 10 2 x 10 = 20 3 x 10 = 30 4 x 10 = 40 5 x 10 = 50 6 x 10 = 60 7 x 10 = 70 8 x 10...
C++. Write a program that asks the user to enter a single word and outputs the...
C++. Write a program that asks the user to enter a single word and outputs the series of ICAO words that would be used to spell it out. The corresponding International Civil Aviation Organization alphabet or ICAO words are the words that pilots use when they need to spell something out over a noisy radio channel. See sample screen output for an example: Enter a word: program Phonetic version is: Papa Romeo Oscar Golf Romeo Alpha Mike The specific requirement...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT