Take the 2-minute tour ×
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap(char* c1,char* c2)  
    char temp=*c1;   

void permutate( char str[], int index)    
    int i = 0;
    static lastChar = 0;

    if( index == strlen(str) )
    { // We have a permutation so print it

    for( i = index; i < strlen(str); i++ )
        if( lastChar == str[i] ) {
        else {
            lastChar = str[i];
        swap( str+index, str+i ); // It doesn't matter how you swap.
        permutate( str, index + 1 );
        swap( str+index, str+i );

 int main(int argc,char** argv)
    return 0;


I am getting a segmentation fault while running this program. The swap function is working fine when I run it on its own.

share|improve this question
Really this code is horrible. Write it again from the beginning! Never use a static variable for that purpose, pass it as a pointer parameter. Never call strlen in a loop, it will compute each time the length and this operation will repeat for each character making the computational complexity horrible. The algorithm itself is wrong and a printf inside a function that is used to compute something is awful, you have always to divide presentation of output from logic! If i was your teacher i would have banned you from classroom :) I didn't downvote because i know that learning can be difficult. –  Salvatore Previti Oct 26 '11 at 17:37
Okay. Actually I did not write this code, just picked it up from a site to learn how to generate all permutations for a string. I agree with what you said, the use of strlen() inside the for loop is horrible. And even I don't understand why the static variable has been used that way. Like I said, I myself don't understand this program very well, so I did not modify it much. –  Arjun J Rao Oct 26 '11 at 17:43
Copy and paste without understanding is even worse. The algorithm to compute this is really simple. However next time you post a new question you should also post every detail about what the code is supposed to do and not only the code itself. If you are learning C i would suggest to try to solve this problem by yourself, u'll learn more and of course you can make it better! –  Salvatore Previti Oct 26 '11 at 17:45
Segmentation fault means you are trying to read or write a portion of memory that is not owned by your process, it means, there is an error on your code that makes your program accessing invalid area of memory. Indeed as I said the code contains a lot of mistakes and of bad things. –  Salvatore Previti Oct 26 '11 at 17:48
putting strlen() in a loop is a typical practice. Any compiler that's smart enough will put the length calculating code right before the loop (almost always as a inline function) –  JosephH Oct 26 '11 at 18:15

2 Answers 2

Look at this function call:


You are trying to modify a string literal. These are read-only and it's the source of the segfault.

share|improve this answer

There are much better ways to do random permutations. Here's an example I found on the web. I've taken the liberty of annotating the code a bit to explain what's going on.

/* this function generates an array containing a permutation of the numbers 0..n-1 */
int * rpermute(int n) {

  /*  first, create an array */
  int k;

  /*  fill it in with the numbers 0..n-1 */
    a[k] = k;

  /*  loop backward through the array */
  for (k = n-1; k > 0; k--) {

    /* swap the k'th element with any element that comes before it (or itself) */
    int j = rand() % (k+1);
    int temp = a[j];
    a[j] = a[k];
    a[k] = temp;
  return a;

This means you only have to loop through the array twice: once to build it, and once to permute it. Also, each permutation has the same probability of occurring, which is the goal if you want to do something like shuffle cards in a deck or scramble a word.

EDIT: remember, the function in this example allocates an array, but you could just as easily pass in an array as a parameter.

EDIT2: FORMATTING! Argh.. always forget about those '<'s

share|improve this answer

Your Answer


