Hackety Hacking Problem Hacking password: Difference between revisions

From OLPC
Jump to navigation Jump to search
(Ok, barely, part of a lesson plan for programming.)
(Add category.)
 
Line 96: Line 96:


</pre>
</pre>
[[Category: Hackety Lesson Plan]]

Latest revision as of 05:00, 6 April 2008

Problem Statement

Hi friends lets talk something about Hacking as this is Hackety hack. Have you ever thought of breaking somebody’s password? Today we will be telling you that.

The situation is like - you know that your friend’s password is made up of some alphabets say { J, O, H, N }, but you don’t know the order in which it is used and you wish to decode his password.

HINT: Print all the permutations that can be made out using these 4 alphabets. And constantly feed the output to the email id whose password is to be known. So you have to write a program to generate all the permutations of these alphabets.

Sample Output

Enter the characters of password abc
Password among the following
abc
acb
bac
bca
cab
cba

Curriculum Connections

This problem gives student help in understanding things mentioned below:

  • How to use Functions and Transfer data between two of them
  • How to swap numbers
  • How to make use of arrays to simplify the code

Solution in C++

#include <stdio.h>
#include<conio.h>
#include<dos.h>

/* function to swap array elements */

void swap (char v[], int i, int j) 
{
	char t;
t = v[i];
	v[i] = v[j];
	v[j] = t;
}

/* recursive function to generate permutations */
void perm (char v[], int n, int i) 
{

	/* this function generates the permutations of the array
	 * from element i to element n-1
	 */
	int	j;

	/* if we are at the end of the array, we have one permutation
	 * we can use (here we print it; you could as easily hand the
	 * array off to some other function that uses it for something
	 */
	if (i == n) 
{
		for (j=0; j<n; j++) printf ("%c ", v[j]);
		printf ("\n");
	} else
		/* recursively explore the permutations starting
		 * at index i going through index n-1
		 */
		for (j=i; j<n; j++) {

			/* try the array with i and j switched */

			swap (v, i, j);
			perm (v, n, i+1);

			/* swap them back the way they were */

			swap (v, i, j);
		}
}

/* little driver function to print permutation of alphabets */

int main () 
     {
	
	Printf(“Enter the characters of password”);
        gets( v);
        int i;
        length=strlen(v); // Length  of password
        printf("Password among the following\n");
	perm (v,length,0);
	getch();
    	return 0;
    }