Home > Enterprise >  Searching for a string in an array without strcmp
Searching for a string in an array without strcmp

Time:01-18

I have a struct in this form:

typedef struct student{
    double student_no;
    char name[50];
    char surname[50];
    double phone_no;
    char mail[50];
};
struct student person[100];

printf("Student's mail address: ");
scanf("%s", person[i].mail); //i use codeblocks and it works without the sign &

First I create an array of mails. Then I get a mail for user, then check if it is in my array that I created in the first place. I have to do it without strcmp(), and that's where I get stuck. Here is what I've tried so far:

char *e1, *e2;
int comp;

typedef struct info{
    char email[50];
};
struct info mailaddress[0];

printf("Enter the mail address to search: ");
scanf("%s", mailaddress[0]);

for (int i = 0; i < u; i  ) {
    for (int j = i   1; j < u; j  ) {
        e1 = person[i].mail;
        e2 = mailaddress[j].email;
        comp = 0;

        while (*e1 && *e2) {
            if (*e1 != *e2) {
                comp = *e1 - *e2;
                break;
            }

              e1;
              e2;
        }

        if (comp == 0)
            printf("%s\t  %s  .lf    .lf      %s\n",
                    person[i].name,
                    person[i].surname,
                    person[i].student_no,
                    person[i].phone_no,
                    person[i].mail);
    }
}

CodePudding user response:

bool streq(char *a, char *b)
{
    while (*a && *b && *a != *b)
        a  , b  ;

    return !*a && !*b;
}

CodePudding user response:

Try like this

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>

#define NO_MAX       20
#define NAME_MAX     20
#define SURNAME_MAX  20
#define PHONE_NO_MAX 20
#define MAIL_NO_MAX  50

struct student{
    char    student_no[NO_MAX   1];
    char    name[NAME_MAX   1];
    char    surname[SURNAME_MAX   1];
    char    phone_no[PHONE_NO_MAX   1];
    char    mail[MAIL_NO_MAX   1];
    struct student *ptr_next;
 };

struct student *ptr_head = NULL;
struct student *ptr_prev;
struct student *ptr_position;


void enter_item(char* label, char* ptr_result, int max){

    // enter item until pressed enter to finished
    char tmp[256];
    do {
        printf("%s: ", label);
        gets(tmp);
    } while((strlen(tmp) > max));
    strcpy(ptr_result, tmp);
}

void enter_list(void) {
    char more;
    struct student *ptr_new;
    do{
        // order dynamic memory
        ptr_new = (struct student *) malloc(sizeof(struct student));
        if(ptr_new) {
            // enter item
            enter_item("student_no", ptr_new->student_no, NO_MAX);
            enter_item("name",       ptr_new->name, NAME_MAX);
            enter_item("surename",   ptr_new->surname, SURNAME_MAX);
            enter_item("phone_no",   ptr_new->phone_no, PHONE_NO_MAX);
            enter_item("mail",       ptr_new->mail, MAIL_NO_MAX);

            // next item
            ptr_new-> ptr_next = ptr_head;
            ptr_head = ptr_new;

            // enter item again or not
            printf("- again(Y/N)? -");
            do{
               more = toupper(getch());
            } while(!(more=='Y' || more=='N'));
            printf(" %c\n\n", more);
        }
        else {
            printf("Memory isn't enough!\n");
            break;
        }
    } while(more == 'Y');
}


void search_mail() {
    // enter mail
    struct student *ptr_search;

    ptr_search = (struct student *) malloc(sizeof(struct student));
    enter_item("mail searching",       ptr_search->mail, MAIL_NO_MAX);
    char *mail = ptr_search->mail;

    // searching mail
    ptr_prev = NULL;
    ptr_position = ptr_head;
    while(ptr_position) {
        if(strcmp(mail, ptr_position->mail) != 0) {
            ptr_prev = ptr_position;
            ptr_position = ptr_position-> ptr_next;
        }
        else {
            printf("Email is found\n");
            return;
        }
    }
    printf("Email isn't found!\n");
}

int main()
{
    enter_list();
    search_mail();
    return 0;
}

Result:

enter image description here

  •  Tags:  
  • Related