Overclock.net banner

String operations in C

635 Views 6 Replies 3 Participants Last post by  thealmightyone
I'm trying to write some functions in C, but I'm having a hard time dealing with them.

I can write these functions easily in C# or java, but I have very limited tools to work with when writing them in C. Can anyone help?

Code:

Code:
// Returns true if shortstr is a subsequence of longstr, 
// and false otherwise. (Example: car is a subsequence of camera, and asdf is a subsequence of askethdeurf)
int subsequence(char shortstr[], char longstr[])
{
    return 0;
}

// Returns true if string1 and string2 are permutatations
// of each other, false otherwise.
int permutation(char string1[], char string2[])
{
    return 0;
}
1 - 7 of 7 Posts
I think I may have a solution to subsequence.

Code:
Code:
// Returns true if shortstr is a subsequence of longstr, 
// and false otherwise.
int subsequence(char shortstr[], char longstr[])
{
    temp = 0;
    int temp2 = 0;
    for (i = 0; i < strlen(shortstr); i++)
    {
        for (j = temp2; j < strlen(longstr); j++) 
        {
            printf("%c == %c %d %d\
\
", shortstr[i], longstr[j], temp, temp2);
            if (shortstr[i] == longstr[j])
            {
                temp++;
                temp2 = j+1;
                break;
            }
        }
    }
    if (temp == strlen(shortstr)) return 1;
    else return 0;
}
Please try to shoot this down with test cases. Thanks


temp2 eliminates recurring letters making temp increase too many times. If you were to run the second for loop for the string "lol"(longstr can be "loel" or something), it would increment temp twice, because j is starting from 0 every time, instead of the letter it used last.
See less See more
Code seems to work fine.

Wrote my own version of subsequence. Tthe permutation function looks interesting, think I'll have to have a go.

Code:
Code:
int subseq(char sub[], char line[])
{
int i, j, match=0, len_sub, len_line;

len_sub=strlen(sub);
len_line=strlen(line);

for(i = 0; i < len_line; i++)
{
if(line[i]==sub[j])
{
j++;
if(j==len_sub)
{
match=1;
break;
}
}
}
return match;
}
See less See more
  • Rep+
Reactions: 1
2
Quote:


Originally Posted by thealmightyone
View Post

Code seems to work fine.

Wrote my own version of subsequence. Tthe permutation function looks interesting, think I'll have to have a go.

Code:
Code:
int subseq(char sub[], char line[])
{
int i, j, match=0, len_sub, len_line;

len_sub=strlen(sub);
len_line=strlen(line);

for(i = 0; i < len_line; i++)
{
if(line[i]==sub[j])
{
j++;
if(j==len_sub)
{
match=1;
break;
}
}
}
return match;
}
It works for you? Maybe my compiler is messed up! I'm using Dev-C++. I had to turn in the assignment with the function "not working correctly". I hope it runs on the TA's computer
.

EDIT: post #444 xD
See less See more
2
Quote:

Originally Posted by chatch15117 View Post
It works for you? Maybe my compiler is messed up! I'm using Dev-C++. I had to turn in the assignment with the function "not working correctly". I hope it runs on the TA's computer
.

EDIT: post #444 xD
Well, then I won't be using that crappy compiler for anything. The code is fine, except of course for #include <string.h>
See less See more
Well I got the assignment back today, and scored a lot higher than I thought I was anticipating(50%). I would have earned a 60% if I had it check if the word was in the dictionary in the first place(these functions are for a spell checker).

Quote:


Author: Velasquez Avila Diego
Date: December 1, 2009 1:11 PM
-10 didn't check if the word was in the dictionary
-25, none of the cases work.
-15, the approach to make the functions wasn't right.

the program was crashing because you didn't declare the array with a static value, when you assign an array with an "*" is declaring the array dynamically, so you will have to allocate space everytime you want to put somethin in that array. The way that I figured out permutation was checking if the letter was in the other word so then I just put a '0' on it and then if I have count = to the lenght of the word is permutation something like this:

int permutation(char string1[], char string2[]) {
char temp[MAXLETTER];
int count=0, i, j;
strcpy(temp,string2);//copy the word to make sure I don't mess with the original one.

for (i=0; i for (j=0; j if ((string1==string2[j])&&(temp[j]!=0)) {

count++;
temp[j]=0;//here is like taking out the letter of the word
break;
}

}

}
if (count==strlen(string1))
return 1;
return 0;
}
I hope this helps
D.V.




I'll try it with the temp[] array and see if the T.A. is correct about dynamically allocating the space.
See less See more
2
Quote:


Originally Posted by chatch15117
View Post

It works for you? Maybe my compiler is messed up! I'm using Dev-C++. I had to turn in the assignment with the function "not working correctly". I hope it runs on the TA's computer
.

EDIT: post #444 xD

Dev-C++ is your IDE. My compiler was GCC. Also, my code requires string.h for the strlen function.

Anyway, your compiler should tell you exactly what's wrong.

I did write a solution to the permutation problem, but deleted it. I used a dynamic arrray to be the same size as the length of the input strings. Remember, free your space when you finish with it.

EDIT:

Code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int permutation(char word1[], char word2[])
{
int len, i, j;
int *tracker;
char c;

if(strlen(word1) != strlen(word2))
{
printf("Input strings are not of equal length.\
");
return 0;
}

len=strlen(word1);

tracker=(int*)malloc(len*sizeof(int));
for(i=0; i<len; i++){tracker[i]=0;} /* Initialise the array */

for(i=0; i<len; i++)
{
c=word1[i];
for(j=0; j<len; j++)
{
if( (word2[j]==c) && (tracker[i]==0) )
{
tracker[i]=1;
break;
}
}
}
for(i=0; i<len; i++)
{
if(tracker[i]==0)
{
return 0;
}
}
return 1;
}

int main(void)
{
if(permutation("hello","lolae"))
{
printf("Strings are permutations.\
");
}
else
{
printf("Strings are not permutations.\
");
}
return 0;
}
I realise this doesn't ask the user for input, but I can't be bothered to interact with the terminal, that's for another function.
See less See more
  • Rep+
Reactions: 1
1 - 7 of 7 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top