logo
  • Home
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C++ Program to Permute All Letters of an Input String

Posted on October 30, 2014 by Manish
This C++ Program Permutes All Letters Of An Input String.

It iterates from the 0th letter to last letter in a string, swaps values and recursively call permute function to print values.

The program has an input as a string. This prints permutation of all letters of an input string.

Here is source code of the C++ Program to Find Permute All Letters of an Input String. The C++ program is successfully compiled and run on g++-4.3.2 on a Linux system. The program output is also shown below.

  1. //This is a C++ Program to Permute All Letters Of An Input String.
  2. #include <iostream>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7. void permute (string temp_str, int start, int end)
  8. {
  9.   int i;
  10.   if (start == end){
  11.     cout << temp_str << " ";
  12.   }
  13.   else{
  14.     for (int i = start; i < temp_str.length (); ++i){
  15.       swap (temp_str[start], temp_str[i]);
  16.       permute (temp_str, start + 1, end);
  17.       swap (temp_str[start], temp_str[i]);
  18.     }
  19.   }
  20. }
  21. int main()
  22. {
  23.   string input_str;
  24.   bool flag = false;
  25.   cout << "Enter String : ";
  26.   cin >> input_str;
  27.   for (int i = 0; i < input_str.length () - 1; ++i)
  28.   {
  29.     if (input_str[i] == input_str[i + 1])
  30.     {
  31.     flag = true;
  32.     break;
  33.     }
  34.     else {
  35.       flag = false;
  36.       break;
  37.     }
  38.   }
  39.   if (flag)
  40.   {
  41.     cout << "The permutation of " << input_str << " is : " << input_str << endl;
  42.   }
  43.   else 
  44.   {
  45.     cout << "The permutations of " << input_str << " are : " << endl;
  46.     permute (input_str, 0, input_str.length () - 1);
  47.   }
  48.   cout << endl;
  49.   return 0;
  50. }

Output :

 
$ g++ PermuteString.cpp
$ ./a.out
 
Enter String : bac
The permutations of bac are : bac bca abc acb cab cba 
Enter String : aaaa
The permutation of aaaaa is : aaaaa
 
Enter String : b
The permutations of b are : b

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
Here’s the list of Best Reference Books in C++ Programming, Data Structures and Algorithms.

« Prev Page - C Program to Implement two Stacks using a Single Array & Check for Overflow & Underflow
» Next Page - C++ Program to Implement Interpolation Search Algorithm
« Java Program to Handle the User Defined Exception Using Throw Keyword
C++ Program to Implement Shell Sort »

Deep Dive @ Sanfoundry:

  1. C Programming Examples using Recursion
  2. Java Programming Examples on Combinatorial Problems & Algorithms
  3. C++ Programming Examples on Combinatorial Problems & Algorithms
  4. C Programming Examples without using Recursion
  5. C Programming Examples on Arrays
  6. C Programming Examples on Combinatorial Problems & Algorithms
  7. Python Programming Examples
  8. C++ Programming Examples on Set & String Problems & Algorithms
  9. C Programming Examples on Strings
  10. C Programming Examples on Set & String Problems & Algorithms
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
TOS & Privacy
Jobs
Bangalore Training
Online Training
SAN Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry