Skip to content
EMPS intranet

Back to top
Hi!   Hi!           Start program

Try stepping through the code


  Value and type of last evaluated expression: Address of array element: Address of array elementx:  (none)

Your browser does not support the canvas element which will make some the features unavailable.

If you are using Internet Explorer within the University of Exeter try going to the Settings menu (probably the gear shape at the top right of this page), selecting "Compatibility View settings", unchecking "Display intranet sites in Compatibility View" and reloading this page.

Code

 Header...
// // Permute a string from p to the end and print out each // permutation starting from string start (which never changes) // Does not avoid duplicates //
void permute(char *string_start,char *p){ » char *swap; » if (p1==0) {// End of string - just print it » printf("%s\n", string_start); » return ; } // permute the string one shorter than this » permute(string_start, p+1); // Go along the string, swapping each element in turn with p » for (swap=p+1; *swap; ++swap) { » char tmp=*swap; » *swap=*p;// swap » *p=tmp; » permute(string_start, p+1); » *p=*swap;// swap back » *swap=tmp; } }
...
int main(int argc,char * *argv){ » char string[8]; » while (1) { » printf("String to permute?\n"); » if (scanf("%7s", string)<1) break; » printf("Permutations of %s:\n", string); » permute(string, string); } » return 0; }
...
Memory

permute-3()

128
129
130
131
132
133
134
135
p = 334
string_start = 332
144
145
146
147
swap = -4292900102

permute-3()

128
129
130
131
132
133
134
135
p = 334
string_start = 332
144
145
146
147
swap = -4292900102

permute-3()

128
129
130
131
132
133
134
135
p = 334
string_start = 332
144
145
146
147
swap = -4292900102

permute-3()

128
129
130
131
132
133
134
135
p = 334
string_start = 332
144
145
146
147
swap = -4292900102

permute-3()

128
129
130
131
132
133
134
135
p = 334
string_start = 332
144
145
146
147
swap = -4292900102

permute-3()

128
129
130
131
132
133
134
135
p = 334
string_start = 332
144
145
146
147
swap = -4292900102

permute-2()

192
193
194
195
196
197
198
199
p = 333
string_start = 332
208
209
210
211
swap = -4292900102

permute-2()

192
193
194
195
196
197
198
199
p = 333
string_start = 332
208
209
210
211
swap = -4292900102

permute-2()

192
193
194
195
196
197
198
199
p = 333
string_start = 332
208
209
210
211
swap = -4292900102

permute-3(): for() {...}-2

207
?

permute-3(): for() {...}

207
?

permute-3(): for() {...}-2

207
?

256
257
258
259
260
261
262
263
p = 332
string_start = 332
272
273
274
275
swap = -4292900102

permute-3(): for() {...}

271
?

permute-3(): for() {...}

271
?

main()

324
325
326
327
argv = 780
328
329
330
331
332
333
334
335
?
?
?
?
string = 'p' 'u' 's' 'h'
336
337
338
339
340
341
342
343
' ' '2' ' ' 'p' 'e' 'r' 'm' 'u'
344
345
346
347
348
349
350
351
't' 'e'
?
?
?
?
?
?
360
361
362
363
argc = 1

NB: the actual memory address of each variable is the
address shown plus 4292900104 (0xFFE07508).

.

Input was:

abc 

Show output (Before looking at the output, work out what you think it put should be and see if you are right.)

Output

String to permute?
Permutations of abc:
abc
acb
bac
bca
cba
cab
String to permute?
                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Share
Back to top