Thread: scrambling words but leaving the first and last letters in the same position

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    scrambling words but leaving the first and last letters in the same position

    Hello Everyone,
    I writing up a program for a class assignment that will create scrambled words by circularly rotating the middle characters of each word by 1 character. Place the scrambled words, 1 per line in an output file.


    in my dict.txt file placed the following words
    computer
    dog
    pepper
    marker
    teapot
    the output file for scramble.txt is the following
    ctmpouer
    odg
    pepper
    kaemrr
    toaept
    the first issue i have is that i can't get to display the output file when i run the program and the scrambling is not working as intended. would i be able to get some help on perfecting this assignment. thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h> /* needed to use exit(1) */
    char* scramble(char* str)
    {
            int i,m,k;
            for(i=1;i<(strlen(str)-1);i++)
            {
                    m = rand() % (strlen(str)-1);
                    k = rand() % (strlen(str)-1);
                    if(m == k)
                    {
                            i--;
                            continue;
                    }
                    char temp = str[k];
                    str[k] = str[m];
                    str[m] = temp;
            }
            return str;
    }
    main()
    {
    char word[80];
    FILE *fpin;
    FILE *fpout;
    /* open the input file -- quit if not found */
    fpin = fopen ("dict.txt", "r");
    if (fpin == NULL)
            {
                    printf("Memmory allocation failed.");
                    exit(1);
            }
    /* open the output file */
    fpout = fopen("scramble.txt", "w");
    while (fscanf (fpin,"%[^\n]\n",word)==1)
    {
    fprintf (fpout,"%s\n", scramble(word));
    }
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Why are using rand()? If you're rotating char's, that is completely non-random.

    When you scramble "pepper" using circular rotation of the letters between the first and last letters only, don't you get: "peeppr"?

    And what about "dog" to "odg"? I don't grok that.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    What i am trying to accomplish is; as long as the first and last letters of each word are in the same place, the middle letters can be arranged in any order, and the words will still be readable.

    sample input file:
    dog
    pepper
    marker
    teapot
    sample output file:
    dog
    pppeer
    mrkear
    tapoet

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I like using a nested for loop pair. Outer one for the word logic, and inner one for the char logic within the word.

    An example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main() { //
       int i,j,len,idx;
       char c;
       char words[4][20]={"Ohio","Utah","Montana","Vermont"};
    
       for(i=0;i<4;i++) {
          len=strlen(words[i])-2;
          for(j=1;j<len;j++) {
             idx = j;
             while(idx==j)
               idx=rand() % len+1; //printf("j: %d  idx: %d ",j,idx); //getchar();
             
             //swap
             c=words[i][j];
             words[i][j] = words[i][idx];
             words[i][idx] = c;      
             //printf("%s\n",words[i]); getchar();
          }
       }
    
       for(i=0;i<4;i++)
          printf("%s ",words[i]);
    
       printf("\n");
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-05-2010, 03:04 AM
  2. Reversing Letters in Words of a Sentence
    By CRSpeedy in forum C Programming
    Replies: 10
    Last Post: 03-31-2009, 06:12 PM
  3. Need Help Scrambling Words for a Game.
    By slickwilly440 in forum C++ Programming
    Replies: 10
    Last Post: 10-08-2005, 10:55 PM
  4. Proccess Termination (Entry Point Scrambling?)
    By johnc in forum C Programming
    Replies: 6
    Last Post: 11-19-2002, 06:22 PM