inserting letter combinations into a phrase

R

Rich

Hi,

I am trying to insert a pattern of letters into a phrase. For example
the phrase is dog, cat, bird, tree. I want to have:
dog, cat, a, a, bird, tree
dog, cat, a, b, bird, tree
dog, cat, a, c, bird, tree
dog, cat, a, d, bird, tree
....
dog, cat, d, c, bird, tree
dog, cat, d, d, bird, tree

What I have now prints:
dog, cat, a, a, bird, tree
dog, cat, b, b, bird, tree
dog, cat, c, c, bird, tree
dog, cat, d, d, bird, tree

I have run splint on the code and it comes back clean so there are no
obvious mistakes. What have I done wrong?
Thanks,

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

#define NUM_OF(x) (sizeof (x) / sizeof x[0])
static size_t holdarea[64];

static int done(char *words[], char *letters, char *pattern) {
size_t i;

for (i=0; i < NUM_OF(*words); i++) {
if ((strncmp(&pattern,"!",1) == 0) && (holdarea <
(strlen(letters)-1))) {
return 0;
}
}
return 1;
}

static void changeup(char *words[], char *letters, char *pattern) {
size_t i;
size_t k;
size_t letlen=0;

if (letters != NULL) {
letlen = strlen(letters)-1;

if (pattern!=NULL) {
for (k=0,i=0; i<strlen(pattern); i++) {
switch (pattern) {
case '!':
if (holdarea < letlen) {
printf("%c",letters[holdarea++]);
}
else {
printf("%c",letters[holdarea]);
holdarea=0;
}
break;
default:
printf("%s",words[k]);
k++;
}
}
}
}
}

int main() {
char *words[]={"Dog","Cat","Bird","Tree"};
char *letters="abcd";
char *pattern="aa!!aa";

while (done(words,letters,pattern)==0) {
changeup(words,letters,pattern);
printf("\n");
}

changeup(words,letters,pattern); //to get last phrase
printf("\n");
return 0;
}
 
E

Eric Sosman

Hi,

I am trying to insert a pattern of letters into a phrase. For example
the phrase is dog, cat, bird, tree. I want to have:
dog, cat, a, a, bird, tree
dog, cat, a, b, bird, tree
dog, cat, a, c, bird, tree
dog, cat, a, d, bird, tree
...
dog, cat, d, c, bird, tree
dog, cat, d, d, bird, tree

What I have now prints:
dog, cat, a, a, bird, tree
dog, cat, b, b, bird, tree
dog, cat, c, c, bird, tree
dog, cat, d, d, bird, tree

I have run splint on the code and it comes back clean so there are no
obvious mistakes. What have I done wrong?
Thanks,

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

#define NUM_OF(x) (sizeof (x) / sizeof x[0])
static size_t holdarea[64];

static int done(char *words[], char *letters, char *pattern) {
size_t i;

for (i=0; i< NUM_OF(*words); i++) {

NUM_OF doesn't do what you want here. See question 6.21 on
the comp.lang.c Frequently Asked Questions (FAQ) web page at
if ((strncmp(&pattern,"!",1) == 0)&& (holdarea<
(strlen(letters)-1))) {


I can't figure out what you're trying to accomplish -- it just
makes no sense to me. What's the relationship between `i' (whose
upper limit is derived from `words', sort of) and `pattern'? Why
do you think the length of `pattern' depends on some attribute of
`words'?
return 0;
}
}
return 1;
}

static void changeup(char *words[], char *letters, char *pattern) {
size_t i;
size_t k;
size_t letlen=0;

if (letters != NULL) {
letlen = strlen(letters)-1;

if (pattern!=NULL) {
for (k=0,i=0; i<strlen(pattern); i++) {
switch (pattern) {
case '!':
if (holdarea< letlen) {
printf("%c",letters[holdarea++]);
}
else {
printf("%c",letters[holdarea]);
holdarea=0;
}
break;
default:
printf("%s",words[k]);
k++;
}
}
}
}
}

int main() {
char *words[]={"Dog","Cat","Bird","Tree"};
char *letters="abcd";
char *pattern="aa!!aa";

while (done(words,letters,pattern)==0) {
changeup(words,letters,pattern);
printf("\n");
}

changeup(words,letters,pattern); //to get last phrase
printf("\n");
return 0;
}


I haven't spent the time to examine your code in detail,
because it's a warm summer afternoon at the start of a holiday
weekend and my brain feels too lazy to try to figure out what
you think these functions are supposed to do. At a guess, the
`pattern' is supposed to emit one of `words' for each 'a' it
contains, while each '!' asks for all of the `letters' to be
emitted, one by one in the midst of the other words and letters.
If so, you want sixteen lines of output from the example -- but
nowhere do I see a loop that will execute sixteen times! The
structure isn't right (not right for the intent I'm guessing at,
anyhow). Maybe a clearer statement of what you're trying to do,
complete with clear statements of what you think each function
is supposed to do, would be helpful.

Now: If I swing my hammock *just* right, I can reach that
piña colada without sloshing it ... Aahhh!
 
R

Rich

I am trying to insert a pattern of letters into a phrase.  For example
the phrase is dog, cat, bird, tree.  I want to have:
dog, cat, a, a, bird, tree
dog, cat, a, b, bird, tree
dog, cat, a, c, bird, tree
dog, cat, a, d, bird, tree
...
dog, cat, d, c, bird, tree
dog, cat, d, d, bird, tree
What I have now prints:
dog, cat, a, a, bird, tree
dog, cat, b, b, bird, tree
dog, cat, c, c, bird, tree
dog, cat, d, d, bird, tree
I have run splint on the code and it comes back clean so there are no
obvious mistakes.  What have I done wrong?
Thanks,
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define NUM_OF(x) (sizeof (x) / sizeof x[0])
static size_t holdarea[64];
static int done(char *words[], char *letters, char *pattern) {
size_t i;
   for (i=0; i<  NUM_OF(*words); i++) {

     NUM_OF doesn't do what you want here.  See question 6.21 on
the comp.lang.c Frequently Asked Questions (FAQ) web page at
     if ((strncmp(&pattern,"!",1) == 0)&&  (holdarea<
(strlen(letters)-1))) {


     I can't figure out what you're trying to accomplish -- it just
makes no sense to me.  What's the relationship between `i' (whose
upper limit is derived from `words', sort of) and `pattern'?  Why
do you think the length of `pattern' depends on some attribute of
`words'?


done is what tells me I have generated all of the phrases. changeup
is supposed to change the letters, i.e. a a should become a b, then a
c, then a d, etc. holdarea is an array of int that stores this
information. When holdarea is 00 it prints aa, when it is 01 it prints
ab, 02 is ac, etc.
     I haven't spent the time to examine your code in detail,
because it's a warm summer afternoon at the start of a holiday
weekend and my brain feels too lazy to try to figure out what
you think these functions are supposed to do.  

Yeah it is a beautiful day here and I wish I was outside instead of
here programming. That might be part of my problem :)

Thank you for pointers. I will read the FAQ now.

Thanks,
 
D

Denis McMahon

What have I done wrong?

Try the following little proglet:

/* proglet starts */

#include <stdio.h>

int main() {
int i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
printf("some words %c %c more words\n",'a' + i, 'a' + j);
}

/* proglet ends */

Rgds

Denis McMahon
 
D

Denis McMahon

I can't be bothered to solve the original problem so I'm going
to make snide remarks about other people's input instead!

To the OP, revised proglet for you to try:

/* proglet starts */

#include <stdio.h>

int main(int argc, char *argv[]) {

int i, j;
char letters[] = "qazw";
char *words[] = {"hat","banana","train","monkey"};
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
printf("%s, %s, %c, %c, %s, %s\n",
words[0],words[1],
letters,letters[j],
words[2],words[3]);
}

/* proglet ends */

Rgds

Denis McMahon
 
R

Rich

done is what tells me I have generated all of the phrases.  changeup
is supposed to change the letters, i.e. a a should become a b, then a
c, then a d, etc.  holdarea is an array of int that stores this
information. When holdarea is 00 it prints aa, when it is 01 it prints
ab, 02 is ac, etc.

Your problem is in the changeup function.
This expression is incrementing the wrong element of holdarea[]

        printf("%c",letters[holdarea++]);


printf("%c",letters[holdarea]++);
seg fault

printf("%c",letters[holdarea[i++]]);
infinite loop

printf("%c",letters[++holdarea]);
prints
DogCatbbBirdTree
DogCatccBirdTree
DogCatddBirdTree

printf("%c",letters[holdarea++]);
won't compile

printf("%c",++letters[holdarea]);
seg fault

printf("%c",letters++[holdarea]);
infinte loop

What did I miss?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top