Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,547 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,563 people online right now. Registration is fast and FREE... Join Now!




Help: Word Scramble & Counter

 
Reply to this topicStart new topic

Help: Word Scramble & Counter

ickiepig
30 Oct, 2007 - 06:38 PM
Post #1

New D.I.C Head
*

Joined: 24 Oct, 2007
Posts: 12


My Contributions
1. what do i need to add to "center" my output ?
2. what kind of array do i need to tell how many words & characters & lines are?

thanks!!

CODE


#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
// #include <ctype>
#include <iostream>
// #include <vector>

using namespace std;

int isinword(char c) {
  return (isalpha(c) || (c==39));  
}

void scramble(char* s) {
  for(int i = 0; i < strlen(s); i++) {
    int position = rand() % strlen(s);

    char temp = s[i];
    s[i] = s[position];
    s[position] = temp;
  }
}

int main(int argc, char *argv[]) {
  
  char filename[200];
  printf("What's the name of the file: ");
  gets(filename);
  if(strlen(filename)==0);
  strcpy(filename,"scramble.txt");

  FILE* f;
  f = fopen(filename,"r");
  if (!f)
    return 1;

  char line[1001];  
  char word[30];
  char midword[30];
  while (fgets(line,1000,f)) {    
    int linelen = strlen(line);
    for (int i=0;i<linelen;i++) {
      if (isinword(line[i])) {    // start a word
        int start = i;
        while (isinword(line[i]))   // look for the end of the word
          i++;
        int end = i;              
        int wordlen = end-start;
        strncpy(word,line+start,wordlen);  
        word[wordlen] = 0;                
        
        if (wordlen > 3) {    
        strncpy(midword,word+1,wordlen-2);  
        midword[wordlen-2] = 0;            
          
        scramble(midword);  // scramble(midword);
        strncpy(line+start+1,midword,strlen(midword));  // use strncpy to copy midword back into line at the appropriate place
        }    
        // keep track of # words, word lengths
      }
    }  
    printf("%s\n",line);  
    // print the modified line (center it on the screen)
    // keep track of # lines
  }
  printf("\n\n");
  
  // print summarys


  fclose(f);

  system("PAUSE");
  return 0;
}



This post has been edited by ickiepig: 30 Oct, 2007 - 07:30 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:34PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month