
S-Language,help with strings,char arrays?
Quote:
>I have to write a C program to generate legal strings in the S-language. A
>string in the S-language consists of the letters A and B and is constructed
>according to the following procedure:
>Consider the character string consisting of the letter S.
>Replace the first S in the character string so far obtained by one of
>SAAS
>ABAB
>ASBB
>AABSSB
>BBAB
>Go to step 1.
>Starting from the string S various character strings can be obtained. For
>example,
>S gives SAAS by rule 1-1.
>This gives ABABAAS by rule 1-2
>This gives ABABAAAABSSB by rule 1-4.
>This gives ABABAAAABBBABSB by rule 1-5.
>This gives ABABAAAABBBABABABB by rule 1-2.
>Write a program that finds all character strings with fewer than 21
characters
>and containing only A's and B's, which can be obtained from S as described
>above. Hint: There are 276 strings
>"This is my code so far,im so lost and confused
> [snip]
I'm not going to answer all your questions, but here are some suggestions
about how to procede:
You're going to have to figure out how to handle strings in C.
There isn't a string type per se in C, just some conventions
about how to use arrays of char and pointers to char for
string operations, and some language features and library
functions that support those conventions.
It's obvious from your code you haven't mastered that material yet.
(Hey, that's why you're taking classes, right?) Studying a good book
on C is the best way I know of to start learning that. I'm certainly not
going to try to explain it from scratch in a newsgroup post.
You'll have to figure that stuff out to get this program working.
As for actually writing the program,
I'd suggest you start by writing a function that just performs the
substitution of a specified string for the first 'S' in another string.
It should take four arguments:
the current string,
a substitution string,
a pointer to an area to store the resulting string,
a maximum length for the resulting string (this will always be 21, so
you could just hard-code it into the function instead of passing it every
time.)
The first three arguments will all be pointers-to-char (char *), but in the
function that calls them, you will have to define arrays of char (char [])
for them. When you learn how to handle strings in C, you'll understand
why. I just mention it to warn you of a common beginner's mistake.
I suggest that the function should return one of three int values,
indicating
one of three things happened:
() The substitution was not performed because there were no 'S's in the
current string. (If the current string was generated according to the
rules,
then it is one of the legal strings you are trying to generate.)
() The substitution was not performed because the resulting string would
have been too long.
() The substitution was performed.
Normally I wouldn't recommend writing on a program bottom-up like this,
but I think this may actually be the most work for you to figure out,
and you won't be able to test the higher-level parts of the program
until you have this low-level stuff working.
Quote:
> Hint: There are 276 strings
> [snip]
> for(i = 0; i <= 276; i++){
I think that hint was to help you determine whether your program was
working. As long as it's producing more or less than 276 output strings,
you know it still needs work. (Of course, just producing the right AMOUNT
of output doesn't prove it's right, but producing the WRONG amount of output
certainly proves its wrong, or possibly that some of the output is
redundant.)
I don't think that hint is going to help you a lot in actually writing the
program,
other than to tell you that if you're going to store the results in an
array, it
needs at least 276 elements.
Quote:
>how would i set it up recursivly,i need a clue or a sterp in the right
>direction
Think about how you would generate these strings if you had to do it
yourself on a chalk board. You need to do it systematically, or
you'll end up overlooking some strings.
When you get all that figured out, and your program is producing more than
276 strings, there is one more problem to overcome.
Hint:
Compare the string you get when you apply the rules in the sequence
1, 1, 2, 2, 2 versus the one you get with 1, 2, 1, 2, 2
(starting with S in both cases).
Then compare the strings you get from 4, 1, 2, 2, 2 versus 4, 2, 1, 2, 2.
-- Gary Culp
--