Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
inserting letter combinations into a phrase
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Rich  
View profile  
 More options Jul 3 2010, 5:09 pm
Newsgroups: comp.lang.c
From: Rich <rtillm...@gmail.com>
Date: Sat, 3 Jul 2010 14:09:29 -0700 (PDT)
Local: Sat, Jul 3 2010 5:09 pm
Subject: inserting letter combinations into a phrase
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;


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Sosman  
View profile  
 More options Jul 3 2010, 5:29 pm
Newsgroups: comp.lang.c
From: Eric Sosman <esos...@ieee-dot-org.invalid>
Date: Sat, 03 Jul 2010 17:29:01 -0400
Local: Sat, Jul 3 2010 5:29 pm
Subject: Re: inserting letter combinations into a phrase
On 7/3/2010 5:09 PM, Rich wrote:

     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
esos...@ieee-dot-org.invalid


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rich  
View profile  
 More options Jul 3 2010, 5:39 pm
Newsgroups: comp.lang.c
From: Rich <rtillm...@gmail.com>
Date: Sat, 3 Jul 2010 14:39:50 -0700 (PDT)
Local: Sat, Jul 3 2010 5:39 pm
Subject: Re: inserting letter combinations into a phrase
On Jul 3, 4:29 pm, Eric 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.

>      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,


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Geoff  
View profile  
 More options Jul 3 2010, 9:11 pm
Newsgroups: comp.lang.c
From: Geoff <ge...@invalid.invalid>
Date: Sat, 03 Jul 2010 18:11:30 -0700
Local: Sat, Jul 3 2010 9:11 pm
Subject: Re: inserting letter combinations into a phrase
On Sat, 3 Jul 2010 14:39:50 -0700 (PDT), Rich <rtillm...@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[]

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Denis McMahon  
View profile  
 More options Jul 3 2010, 11:46 pm
Newsgroups: comp.lang.c
From: Denis McMahon <denis.m.f.mcma...@googlemail.co.uk>
Date: Sun, 04 Jul 2010 04:46:40 +0100
Local: Sat, Jul 3 2010 11:46 pm
Subject: Re: inserting letter combinations into a phrase
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Heathfield  
View profile  
 More options Jul 4 2010, 3:56 am
Newsgroups: comp.lang.c
From: Richard Heathfield <r...@see.sig.invalid>
Date: Sun, 04 Jul 2010 08:56:18 +0100
Local: Sun, Jul 4 2010 3:56 am
Subject: Re: inserting letter combinations into a phrase

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Denis McMahon  
View profile  
 More options Jul 4 2010, 11:03 am
Newsgroups: comp.lang.c
From: Denis McMahon <denis.m.f.mcma...@googlemail.co.uk>
Date: Sun, 04 Jul 2010 16:03:23 +0100
Local: Sun, Jul 4 2010 11:03 am
Subject: Re: inserting letter combinations into a phrase
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]);
  }

/* proglet ends */

Rgds

Denis McMahon


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rich  
View profile  
 More options Jul 4 2010, 3:15 pm
Newsgroups: comp.lang.c
From: Rich <rtillm...@gmail.com>
Date: Sun, 4 Jul 2010 12:15:29 -0700 (PDT)
Local: Sun, Jul 4 2010 3:15 pm
Subject: Re: inserting letter combinations into a phrase
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>
> 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[]

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

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?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »