Manipulating a large, random string

This is a discussion on Manipulating a large, random string within the C Programming forums, part of the General Programming Boards category; I can't quite wrap my head around a good approach to to randomly printing a word (as a string of ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    8

    Lightbulb Manipulating a large, random string

    I can't quite wrap my head around a good approach to to randomly printing a word (as a string of characters) into an already printed box of random letters. I figure this would all be done to a file, but considering I'm an amateur at file manipulation, this is difficult.

    I can construct the box of letters:

    Code:
    void bld(char str[11]){
    int i, j, rng; char alpha[27] = "abcdefghijklmnopqrstuvwxyz"; FILE *fp; fp = fopen("foo.txt", "w")
    for (i=0; i < VER; i++){
    for (j=0; j < HOR; j++){
    rng = rand() % 26;
    fprintf(fp, "%c", alpha[rng]);
    }
    fprintf(fp, "\n");
    } fclose(fp);
    return;
    }
    From here, I'm at a mental road block.

    Does anyone know any good methods for modifying the printed string?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,594
    It sounds like what you want to do is to keep the string in memory, manipulate it, and then print it when done.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    Quote Originally Posted by laserlight View Post
    It sounds like what you want to do is to keep the string in memory, manipulate it, and then print it when done.
    Hmm... seems reasonably easier. I wasn't sure if I could get away with that.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It's unclear what you want. It looks like you're creating a 2D array of random characters. Do you want to overwrite some of those with a word, like this? (The x's are random chars.)
    Code:
    x x x x
    x x x x
    x H I x
    x x x x
    If so, do you need to do it in different directions, like diagonally?
    Code:
    x x x x
    x H x x
    x x I x
    x x x x

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    That's exactly it.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try writing (and posting) some code to fill a HOR by VER sized character array with random letters.

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    How's this?

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    
    #define PAUSE   system("PAUSE")
    #define CLEAR   system("CLS")
    #define HOLD    getchar()
    #define MAX     11
    #define SET     27
    #define HOR     30
    #define VER     10
    
    
    int main(){
        int i, j, rng;
        char grd[HOR][VER] = {'\0'},
        alpha[SET] = "abcdefghijklmnopqrstuvwxyz";
    
    
        for (i = 0; i < VER; i++){
            for (j = 0; j < HOR; j++){
                rng = rand() % VER;
                *grd[j] = alpha[rng];
                printf("%c", *grd[j]);
            }
            printf("\n");
        }
    
    
    
    
        HOLD;
        CLEAR;
        PAUSE;
    
    
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Almost got it!

    Change the inner part to:

    Code:
    for (i = 0; i < VER; i++) {
        for (j = 0; j < HOR; j++) {
            grd[i][j] = (rand() % ('z' - 'a')) + 'a'; 
            printf("%c", grd[i][j]);
        }
        printf("\n");
    }
    And lose the HOLD/CLEAR/PAUSE calls, not only is it non-portable, the macros make it confusing.
    Last edited by memcpy; 02-20-2012 at 08:19 PM.

  9. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    Huh, this is definitely more efficient.

    Thanks for the tip on the macros, they're a force of habit.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    That looks pretty good, except that you're only writing to the first "row" of the array. See memcpy's post.

    Try separating it out into a function.

    And write a separate function to print it out.

    Then try making a function to insert the word.

    Also, you should have a call to srand early in main:
    Code:
    srand(time(0))

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Manipulating String of Struct Array in Functions
    By seannabeast in forum C Programming
    Replies: 5
    Last Post: 11-16-2011, 07:34 PM
  2. manipulating string pointer
    By kmirza in forum C Programming
    Replies: 4
    Last Post: 07-19-2010, 01:54 AM
  3. Manipulating words in a string of characters
    By gp364481 in forum C Programming
    Replies: 9
    Last Post: 09-10-2008, 09:29 AM
  4. string-manipulating function
    By Linette in forum C++ Programming
    Replies: 7
    Last Post: 04-01-2002, 05:48 PM
  5. Manipulating a string
    By pabellon in forum C Programming
    Replies: 1
    Last Post: 12-19-2001, 05:19 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21