user6093969
user6093969

Reputation:

Output not as expected

I am supposed to write a program to extract Web addresses starting with www. and ending with .edu. The program displays Web address contained in the input entered by the user. If the input does not contain a web address that starts with www. and ends with .edu, the program should display a message that indicates such a web address cannot be found.

Input: http://www.usf.edu/admission
Output: www.usf.edu
Input: https://www.facebook.com/
Output: Web address starting with www. and ending with .edu not found

However when my program runs, it is not displaying the correct output. I don't have any compiler errors or warnings so I'm not sure where the issue could be.

// This program extracts the text from the website URL
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define STR_LEN 1000

void read_line(char *str, int n);
void pass_check(char *str);
void extract(char *s1, char *s2);

int main(void)
{
    char instr[STR_LEN + 1];
    char outstr[STR_LEN + 1];

    printf("Please enter a URL: ");
    read_line(instr, STR_LEN);
    extract(instr, outstr);

    puts(outstr);
    pass_check(outstr);

    return 0;
}

void extract(char *s1, char *s2) {
    char *p, *q;
    q = s2;
    for (p = s1 + 7; *p != 0; p++) {
        if (*p == '/')
            break;
        else {
            *q = *p;
            q++;
        }
    }
    *q = '\0';
    *p = '\0';
}

void read_line(char *str, int n) {
    int ch;
    int i = 0;
    while ((ch = getchar()) != '\n') {
        if (i < n) {
            *str++ = ch;
            i++;
        }
    }
    *str = '\0';
}

void pass_check(char *str) {
    const char *fref = "www";
    const char *lref = "edu";

    int len = strlen(str);
    printf("%d", len);

    char *l = &str[len - 3];
    char f[STR_LEN + 1];

    strncpy(f, str, 3);

    if ((strcmp(f, fref) == 0) && strcmp(l, lref) == 0) {
        printf("Output: ");
        puts(str);
        printf("\n");
    } else
        printf("Please only insert a .edu URL.");
}

Upvotes: 2

Views: 134

Answers (1)

chqrlie
chqrlie

Reputation: 144695

The function strncpy() does not do what you think it does: strncpy(f, str, 3); will not append a null byte to f, so strcmp(f, fref); will actually have undefined behavior as f is uninitialized beyond the first 3 bytes.

Do not use this function, learn why from these blogs:

Also note that your readline() function will run an infinite loop is the file is empty or not terminated by a newline.

Here is a corrected version:

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

#define STR_LEN 1000

void read_line(char *str, size_t n);
int extract(const char *str, char *dest);

int main(void) {
    char instr[STR_LEN + 1];
    char outstr[STR_LEN + 1];

    printf("Please enter a URL: ");
    read_line(instr, sizeof(instr));
    if (extract(instr, outstr)) {
        puts(outstr);
    } else {
        printf("Web address starting with www. and ending with .edu not found\n");
    }
    return 0;
}

int read_line(char *str, size size) {
    int ch;
    size_t i = 0;
    while ((ch = getchar()) != EOF && c != '\n') {
        if (i + 1 < size) {
            str[i++] = ch;
        }
    }
    str[i] = '\0';
    return (ch == EOF && i == 0) ? EOF : i;
}

int extact(const char *str, char *dest) {
    const char *p;

    *dest = '\0';

    for (;;) {
        if ((p = strstr(str, "https://www.")) != NULL) {
            p += 8;  // skip the https:// prefix
        } else 
        if ((p = strstr(str, "http://www.")) != NULL) {
            p += 7;  // skip the http:// prefix
        } else {
            break;
        }
        // URL starts with www.
        size_t len = strcspn(p, "/ \n");  // compute length of website name
        if (len > 8 && !memcmp(p + len - 4, ".edu", 4)) {
            // copy website name, assuming dest is at least as large as str
            strncat(dest, p, len);
            return 1;
        }
        str = p + len;
    }
    return 0;
}

Upvotes: 1

Related Questions