Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with C Programming assignment
#21
Devil's Sunrise Wrote:There are some other ways I can think of right now, both seem to be possibly faster:
Set up two arrays with 256 slots.
Whenever "a" is found in any string, increase the "a"-value for that string's array. Do that for all values. When null is found, check that the two arrays you've made is equal.

Set up a list or a hash-array. Repeat the same thing as written above.

honestly, i dont know enough to know how to do that. And i kept trying to sort the strings and kept getting various problems. currently have something working, doubt it's perfect.
 Spoiler

edit: posted what i have that seems to work for permutation, now working on main.
have got it to read the dictionary into a string array, but cant get it to acknowledge that a word is in the dictionary, even like the first word. Keeps coming back false for the test.
Reply
#22
strcmp()

Also, for the permutation code, be careful with strings that have duplicate letters. permutation("aaaaab", "abbbbb") returns true with your code, I think.

The way I first thought of was DS's method; set up an array of size 26. It'd be something like this:

Code:
int count[26];

for (i = 0; string1[i] != '\0'; i++)
{
  int ch = string1[i] - 'a';
  count[ch]++;
}

for (i = 0; string2[i] != '\0'; i++)
{
  int ch = string2[i] - 'a';
  count[ch]--;
}

for (i = 0; i < 26; i++)
{
  if (count[i] != 0)
    return false;
}

return true;

The basic idea is to keep a tally of how many of each letter there are. For example, count[0] is how many a's there are, count[25] is how many z's there are. First it goes through string1 and tallies up each letter, then it goes through string2 and crosses off each letter. If they are permutations of each other, these should cancel out exactly, so everything in count[] should be 0. Then all you have to do is loop through count[] and look for something that isn't 0, because if there is, you know they're not permutations.

But make sure that count[] is initialized at 0, or you get arbitrary values everywhere and messes up. I think
Code:
int count[26] = {0}
will do the trick.
Reply
#23
Russt Wrote:strcmp()

Also, for the permutation code, be careful with strings that have duplicate letters. permutation("aaaaab", "abbbbb") returns true with your code, I think.

The way I first thought of was DS's method; set up an array of size 26. It'd be something like this:

Code:
int count[26];

for (i = 0; string1[i] != '\0'; i++)
{
  int ch = string1[i] - 'a';
  count[ch]++;
}

for (i = 0; string2[i] != '\0'; i++)
{
  int ch = string2[i] - 'a';
  count[ch]--;
}

for (i = 0; i < 26; i++)
{
  if (count[i] != 0)
    return false;
}

return true;

The basic idea is to keep a tally of how many of each letter there are. For example, count[0] is how many a's there are, count[25] is how many z's there are. First it goes through string1 and tallies up each letter, then it goes through string2 and crosses off each letter. If they are permutations of each other, these should cancel out exactly, so everything in count[] should be 0. Then all you have to do is loop through count[] and look for something that isn't 0, because if there is, you know they're not permutations.

But make sure that count[] is initialized at 0, or you get arbitrary values everywhere and messes up. I think
Code:
int count[26] = {0}
will do the trick.

thanks, an i guess i knew to a degree that aaaab and bbbba would show up correct, i just couldnt think of a way around it and realized that that error probably wouldnt have shown up much, due to how few situations it would arise in, and considering i now have 10 hours to spend thanksgiving with family, get a working substring and main (though substring isnt technically necessary) and submit the project.
Reply
#24
ok, believe i have everything working to an extent, only problem seems to be that subsequence has an issue with duplicate letters as well, as iii and icc get returned as subsequences of peice (i used the example in the assignment, dont ask me why iii and icc are in the dictionary.txt file)
can anyone point out what i can change to fix that? Because im close enough now that part of me just wants to turn the thing in and have done.
Code:
int subsequence(char shortstr[], char longstr[])
{
    int i, j;
    j=0;
    
    for (i = 0; longstr[i]; i++){
        while (shortstr[j]) {
            if (longstr[i] != shortstr[j])
            break;
            else
            j++;
            }
                      
        }
    if (shortstr[j] == '\0')
            return 1;
            else
            return 0;
}
Reply
#25
That inner while loop should be changed to an if instead. The way you have it now, it keeps matching characters in shortstr with the same character in longstr, so iii gets matched with the i in peice.

Actually, there are a number of things wrong there...

Code:
for (i = 0; longstr[i]; i++) {
  if (shortstr[j] == '\0')
    break; // end of shortstr
  if (longstr[i] == shortstr[j])
    j++; // match against the next character
}

return (shortstr[j] == '\0') // true if all of shortstr was matched, false otherwise
Reply
#26
I highly suggest you review learn how to do loops. Breaking conditions are there for a reason; that reason is not to stick a random letter there.
Reply
#27
JoeTang Wrote:I highly suggest you review learn how to do loops. Breaking conditions are there for a reason; that reason is not to stick a random letter there.
Strings are null-terminated, so the end of the string breaks the loop.

It's a lazy way to do things, but it works so why not.
True, the nicer way to do it would be
Code:
for (i = 0; i < strlen(longstr); i++)
but it accomplishes the same thing as
Code:
for (i = 0; longstr[i][color=gray] != '\0'[/color]; i++)
anyway.
Reply
#28
thank you, everyone, for all your help. Finally finished and submitted.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)