开发者

Comparing strings with user-created string class

For this assignment I had to create my own string class. I initially wrote the compareto method to compare two string but return whichever is overall larger. What I want to do is compare and return which one is alphabetically larger i.e. comparing two strings, for example: smith and htims. With the way I designed the compareto method is that the result will be that they are equal. What I want to do is tell me which one comes first alphabetically, so for my example htims would come first. I understand how to do this in Java or even in C with using the <string.h> library, I am just confused as to how to do this myself.

EDIT: I just wanted to note that I am not looking for code answer, rather a nudge in the how I should write the code.

int compareto(void * S1, void * S2){
    String s1 = (String S1);
    String s2 = (String S2);
    int i, cs1 = 0, cs2 = 0; //cs1 is count of s1, cs2 is count of s2

    while(s1->c[i] != '\0'){ //basically, while there is a word
        if(s1->c[i] < s2->c[i]) // if string 1 char is less than string 2 char
            cs2++; //add to string 2 count
        else (s1->c[i] > s2->c[i]) //vice versa
            cs1++;
        i++;
    }

//for my return I basically have

        if(cs1>cs2){
         return 1;
    }
    else if(cs2 > cs1){
         return 2;
    }
    return 0;

here is mystring.h

typedef struct mystring {
    char * c;
    int length;

    int (*sLength)(void * s);
    char (*charAt)(void * s, int i);
    int (*compareTo)(void * s1, void * s2);
    struct mystring * (*concat)(void * s1, void * s2);
    struct mystring * (*subString)(void * s, int begin, int end);
    void (*printS)(void * s);

} string_t;
typedef string_t * String;

Any suggestions, all of my google searches involve using the <string.h> library, so I've had no luck.

Im using this to traverse through a linked list and remove the person whose last name matches the person the user is trying to delete.

Here is my test code to help clarify my problem (Note that compareto is in the remove function):

int main() {
    Node startnode, currentnode, newnode;
    int ans, success;
    String who;
    who = newString2();

    startnode = (Node) malloc(sizeof(pq_t));
    startnode->next = NULL;
    currentnode = startnode;
    ans = menu();
    while (ans != 0) {
        switch (ans) {
        case add:
            newnode = getStudent();
            startnode = insert(newnode, startnode);
            break;
        case remove:
            printf("Enter the last name of the person you want to delete : \n");
            scanf("%s", &who->c);
            startnode = removeStudent(startno开发者_高级运维de, who, &success);
            if (success == 0)
                printf("UNFOUND\n");
            else
                printf("permanently DELETED\n");
            break;

        case view:
            printf("Now displaying the list : \n");
            displaylist(startnode);
            break;
        }
        ans = menu();
    }
}

Node removeStudent(Node head, String who, int * success) {
    Node p, l; //p = pointer node, l = previous node
    Student current; //Im using generics, so I have to case the current node->obj as a student.
    String ln, cln; //the last name of the person I want to remove, and the last name of the current node

    p = head;
    l = p;
//there can be three cases, p->first node, p->last node, p->some node in between
    if (head->obj == NULL) { 
        printf("The list is empty\n"); //when the list is empty
        *success = 0;
        return NULL;
    }
    while (p != NULL) {
        current = (Student) p->obj;
        cln = current->ln;
        if (ln->compareTo(who, cln) == 0) {
            if (head == p) { //when there is only one node
                head = head->next;
                free(p);
                *success = 1;
                return head;
            } else if (p->next == NULL) { //last node
                l->next = NULL;
                free(p);
                *success = 1;
                return head;
            } else {
                l->next = p->next; //middle
                free(p);
                *success = 1;
                return head;
            }
        }
        l = p;
        p = p->next;
    }
    *success = 0;
    return head;//couldnt find the node
}


Try comparing the following pairs of strings:

"ABC" vs "DEF"

"ADF" vs "BBB"

"ABC" vs "CBA"

What results do you get? More importantly, why? How do these results compare to what you want to get?

(You should first work it out in your head. Work out the values of c1 and c2 for each step of the comparison loop.)


First, ln isn't properly initialized in the sample removeStudent(), so calling ln->compareTo will probably cause a segfault. Hopefully, ln is properly initialized in your actual code.

To define an ordering on the strings, you can first define what's known in database circles as a "collation": an ordering on characters. You can implement the collation as a function (called, say, chrcmp), or inline within your string comparison function. The important thing is to define it.

Generally speaking, an ordering on a type induces a lexicographic order on sequences of that type: to compare two sequences, find the first place they differ; the lesser sequence is the one with the lesser element at that position.

More formally, suppose sequences are indexed starting at 0. let a and b be sequences of the base type of lengths m and n, respectively. The lexicographic order a ≤ b is:

  • a < b where ai R bi and aj=mj for all 0 ≤ j < i
  • a < b if a is a prefix of b
  • a=b if m=n and ai=bi for all 0 ≤ i < m

Where "a is a prefix of b" means m < n and ai = bi for all 0 ≤ i < m.

The advantage of this approach is you can write a comparison function that will work with any homogeneous sequence type: strings, lists of strings, arrays of integers, what-have-you. If you specialize the comparison function for null-terminated strings, you don't need to worry about the cases for prefixes; simply have '\0' be the least character in the collation.

From a general comparison function (called, say, lexiCompare), you can define

lexicCompareString (a, b):
    return lexicCompare(a, b, chrcmp)

and set a String's compareTo member to lexicCompareString.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜