C PROGRAMMING Word Scramble 1 Write a C function with one pa
C PROGRAMMING: Word Scramble
1. Write a C function with one parameter, a pointer to a string. The function scrambles the characters in the string, that is, it randomly permutes the characters in the string. Your function should follow this prototype:
void scramble(char *string);
For example, if the string was originally “pumpkin”, after scrambling it might be “kpmunip”. Do this by repeatedly randomly picking two characters in the string and swapping them. Use the rand() function to compute random indices into the string. Initialize rand() with srand()in main().
Don’t assume that the string has any particular length. Scramble the string \"in place,\" that is, alter the string that the function is called with.
Test your function on a variety of strings. Note: do not use a string literal as an argument to this function, since string literals cannot be changed.
2. Write a program that plays a game:
The original is an un-scrambled word. Then make a scrambled version of the same word. The program shows the scrambled version to the user, and then repeatedly asks the user to guess the original word. If the user correctly guesses the word in three tries (or less) print a winning message and exit. Use gets() to get the guess. If the user fails to guess the word in three tries, print a losing message and exit. Use strcmp() to compare strings.
For this assignment, it is probably best to use array indexing (i.e. expressions like string[j]) for accessing characters in the string. You will also need header files string.h, ctype.h, stdio.h, stdlib.h, and time.h.
main()should have an array of possible original words, something like:
char *words[NUMWORDS] = { \"pumpkin\", \"cantalope\", \"watermelon\", \"apple\", \"kumquat\" };
String literals cannot be changed, so use strcpy() to copy characters from a string literal to a malleable array of chars. main()will also need a buffer for the scrambled version of the word.
char original[WORDSIZE], scrambled[WORDSIZE];
srand( time(NULL) );
strcpy( original, words[ rand()%NUMWORDS ] );
Document nicely, use sensible layout and indenting, use sensible variable names, and test thoroughly. Avoid global variables (global constants, however, are encouraged). Turn in your final program using the Blackboard assignments tool.
Solution
Answer:
Note: User given template is used.
//include needed header files
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<time.h>
//Define constants
#define WORDSIZE 12
#define NUMWORDS 5
//method to scramble a string
void scramble(char *string)
{
int k1,k2,k3;
char tt;
//get length of string
k1=strlen(string);
//loop to randomly scramble the string
for(k2=0;k2<k1;k2++)
{
k3=rand()%k1;
tt=string[k2];
string[k2]=string[k3];
string[k3]=tt;
}
}
//main
int main()
{
//no. of tries
int tries=3;
//word array
char *words[NUMWORDS] = {\"pumpkin\", \"cantalope\", \"watermelon\", \"apple\", \"kumquat\" };
char original[WORDSIZE], scrambled[WORDSIZE];
char userString[WORDSIZE];
srand( time(NULL) );
//original string
strcpy( original, words[ rand()%NUMWORDS ] );
//scrambled word
strcpy( scrambled, original );
//call method to scramble
scramble(scrambled);
printf(\"The scrambled word is %s\ \", scrambled);
//Loop to get user for atmost 3 tries
for(int kk=0;kk<tries;kk++)
{
//get user guess
printf(\"Enter your guess:\");
gets(userString);
//Compare user string with original string
if(strcmp(original,userString)==0)
{
//If matches then print correct
printf(\"You guessed correctly\ \");
exit(0);
}
}
//Print user losed the game
printf(\"You lose\ \");
exit(0);
}
Sample output:
sh-4.3$ gcc -o main *.c
sh-4.3$ main
The scrambled word is antclpoea
Enter your guess:apple
Enter your guess:pumpkin
Enter your guess:kumquat
You lose
sh-4.3$ gcc -o main *.c
sh-4.3$ main
The scrambled word is umpkipn
Enter your guess:kumquat
Enter your guess:apple
Enter your guess:pumpkin
You guessed correctly



