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;
}
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;
}