Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Passing Strings && randomizing

c string

  • Please log in to reply
14 replies to this topic

#13 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash

Posted 23 October 2012 - 11:52 AM

Are you sure that you're supposed to scramble each word, or just scramble the list?
  • 0
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#14 Pally

Pally

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 413 posts
  • Programming Language:C, Java, PHP, Python, PL/SQL, Assembly
  • Learning:C++, C#, Lisp, Scheme

Posted 23 October 2012 - 01:29 PM

Are you sure that you're supposed to scramble each word, or just scramble the list?


Yes scramble each word independently, However now I have a new issue.. I need to trim the white space out if a user enters a guess like "gussing "

here is all my code.. everything works except the trim function.. not sure how to implement this..

Thank you again!


#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

char *scrambled;
char *original;
char input[20];

void trim(char *string){
int len = strlen(string)+1;
int i, space = 0, c;
char *fix = (char *) malloc(len);
for(i=0;i<len;i++){
c = fix[i];
if(isspace(c)){
space = 1;
}
}
printf("\ntestrrr: %d", space);
}


void scramble(char *string){
scrambled = (char *) malloc(strlen(string)+1);
char temp;
int num1,num2, i;
strcpy(scrambled, string);
srand(time(NULL));
for(i = 0;i<=200;i++){ //here I'll randomize and scrambled word
num1 = rand()%(strlen(string));
num2 = rand()%(strlen(string));
temp = scrambled[num1];
scrambled[num1] = scrambled[num2] ;
scrambled[num2] = temp;
}
}

void scrambleGame(char *original, char *scrambled){
printf("\n\nOriginal: %s Scrambled: %s", original, scrambled);

int i = 0, correct = 0;
printf("\n\nYou have three guesses..GOOD LUCK!\n\nUnscramble this word: %s", scrambled);
while(i < 3 && correct == 0){
printf("\n\n\n\nYour Guess: ");
gets(input);
trim(input);
if(strcmp(original, input) == 0){
printf("\n\nCongratulations You Win This Round!");
correct++;
}
else if(i != 2){
printf("\nNOPE..Try again");
}
i++;
}
if(correct == 0){
printf("\n\nSorry your a loser this round");
}
}


void main(){
int select,done = 0;
const int totalWords = 32;
char *words[32] = { "pumpkin", "cantalope", "watermelon", "apple", "kumquat", "sixteen", "blue", "phone", "juice", "notebook", "telephone", "baseball", "video", "programming", "string", "function", "time", "table", "paper", "elevator", "movie", "computer", "soda", "light", "photography", "mexico", "africa", "united", "legal", "traffic", "glass", "learning"};
char check[20];
printf("Welcome to the Word scrambler Game. You will be shown a scrambled word. unscramble the letters and guess the word. You have three tries per word.");
srand(time(NULL));
select = rand()%(totalWords);
original = words[select];
scramble(original);
scrambleGame(original,scrambled);
while(done != 1){
printf("\n\nWould you like to play again? 'Y' or 'N' ");
gets(check);
if(strcmp("Y", check) == 0){
srand(time(NULL));
select = rand()%(totalWords);
original = words[select];
scramble(original);
scrambleGame(original,scrambled);
check[0] = 'X';
}
else{
done = 1;
}
}
}

  • 0
Your Friendly Neighborhood Pally

#15 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 23 October 2012 - 01:45 PM

Either use strtok() to extract the string as a token delimited by whitespace characters, or implement it with loops. isspace() from ctype.h will be of some help to you here.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid






Also tagged with one or more of these keywords: c, string

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download