Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

inserting letter combinations into a phrase

30 views
Skip to first unread message

Rich

unread,
Jul 3, 2010, 5:09:29 PM7/3/10
to
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[i],"!",1) == 0) && (holdarea[i] <
(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[i]) {
case '!':
if (holdarea[i] < letlen) {
printf("%c",letters[holdarea[i]++]);
}
else {
printf("%c",letters[holdarea[i]]);
holdarea[i]=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;
}

Eric Sosman

unread,
Jul 3, 2010, 5:29:01 PM7/3/10
to

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
<http://www.c-faq.com/>.

> if ((strncmp(&pattern[i],"!",1) == 0)&& (holdarea[i]<
> (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'?

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!

--
Eric Sosman
eso...@ieee-dot-org.invalid

Rich

unread,
Jul 3, 2010, 5:39:50 PM7/3/10
to

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,

Geoff

unread,
Jul 3, 2010, 9:11:30 PM7/3/10
to
On Sat, 3 Jul 2010 14:39:50 -0700 (PDT), Rich <rtil...@gmail.com>
wrote:

>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[]

Denis McMahon

unread,
Jul 3, 2010, 11:46:40 PM7/3/10
to
On 03/07/10 22:09, Rich wrote:

> 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

Richard Heathfield

unread,
Jul 4, 2010, 3:56:18 AM7/4/10
to
Denis McMahon wrote:
> On 03/07/10 22:09, Rich wrote:
>
>> 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);

What you have done wrong is to assume that 'a' is followed by at least
three printable character sets, which isn't necessarily true.

Other than that, it's hard to guess what you have done wrong without
knowing what you were trying to do.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within

Denis McMahon

unread,
Jul 4, 2010, 11:03:23 AM7/4/10
to
On 04/07/10 08:56, Richard Heathfield wrote:

> 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[i],letters[j],
words[2],words[3]);

Rich

unread,
Jul 4, 2010, 3:15:29 PM7/4/10
to
On Jul 3, 8:11 pm, Geoff <ge...@invalid.invalid> wrote:
> On Sat, 3 Jul 2010 14:39:50 -0700 (PDT), Rich <rtillm...@gmail.com>

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

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

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

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

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

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

What did I miss?

0 new messages