Thread: Manipulating a large, random string

  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
    28,413
    It sounds like what you want to do is to keep the string in memory, manipulate it, and then print it when done.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 09: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, 08: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, 06:48 PM
  5. Manipulating a string
    By pabellon in forum C Programming
    Replies: 1
    Last Post: 12-19-2001, 06:19 PM

Tags for this Thread