Esempio n. 1
0
int main(int argc, char * * argv)
{
    printf("Welcome to PA02.\n"
	   "\n"
	   "You are encouraged to edit this file in order to test\n"
	   "the behavior of the functions you write in answer02.c\n"
	   "\n"
	   "This file will not be marked, and should not be\n"
	   "submitted.\n"
	   "\n"
	   "Don't forget to post questions on blackboard, and ask\n"
	   "the TAs and your classmates for help.\n"
	   "\n");

    const char * s1 = "Hello World!";
    const char * s2 = "";
    const char * s3 = "foo";

    // -- my_strlen, should be: 12, 0, and 3
    printf("my_strlen(\"%s\") = %d\n", s1, (int) my_strlen(s1));
    printf("my_strlen(\"%s\") = %d\n", s2, (int) my_strlen(s2));
    printf("my_strlen(\"%s\") = %d\n", s3, (int) my_strlen(s3));

    // -- my_countchar, should be: 3, 0, and 2
    printf("my_countchar(\"%s\", 'l') = %d\n", s1, (int) my_countchar(s1, 'l'));
    printf("my_countchar(\"%s\", 'o') = %d\n", s2, (int) my_countchar(s2, 'o'));
    printf("my_countchar(\"%s\", 'o') = %d\n", s3, (int) my_countchar(s3, 'o'));

    // -- my_strchr, should be: "llo World!", "(null)", and ""
    printf("my_strchr(\"%s\", 'l') = %s\n", s1, my_strchr(s1, 'l'));
    printf("my_strchr(\"%s\", 'o') = %s\n", s2, my_strchr(s2, 'o'));
    printf("my_strchr(\"%s\", '\\0') = %s\n", s3, my_strchr(s3, '\0'));

    // -- my_strstr, should be: "World!", "Hello World!", "(null)"
    printf("my_strstr(\"%s\", \"World\") = %s\n", s1, my_strstr(s1, "World"));
    printf("my_strstr(\"%s\", \"\") = %s\n", s1, my_strstr(s1, ""));
    printf("my_strstr(\"%s\", \"hello\") = %s\n", s1, my_strstr(s1, "hello"));

    // -- my_strcpy. For this function you need a buffer where you
    // copy the string to. 
    char buffer[BUFFER_LEN];
    my_strcpy(buffer, s1);
    printf("my_strcpy(buffer, \"%s\"), buffer = \"%s\"\n", s1, buffer);
    my_strcpy(buffer, s2);
    printf("my_strcpy(buffer, \"%s\"), buffer = \"%s\"\n", s2, buffer);
    my_strcpy(buffer, s3);
    printf("my_strcpy(buffer, \"%s\"), buffer = \"%s\"\n", s3, buffer);

    // -- my_strcat. You will have to do this yourself... just
    // look at my_strcpy for an example, and go from there.

    // -- my_isspace. You will have to do this for yourself.

    // -- my_atoi. You will have to do this for yourself.

    return EXIT_SUCCESS;
}
Esempio n. 2
0
int main(int argc, char * * argv)
{
    printf("Welcome to PA02.\n"
	   "\n"
	   "You are encouraged to edit this file in order to test\n"
	   "the behavior of the functions you write in answer02.c\n"
	   "\n"
	   "This file will not be marked, and should not be\n"
	   "submitted.\n"
	   "\n"
	   "Don't forget to post questions on blackboard, and ask\n"
	   "the TAs and your classmates for help.\n"
	   "\n");

    const char * s1 = "Hello World!";
    const char * s2 = "";
    const char * s3 = "foo";

    // -- my_strlen, should be: 12, 0, and 3
    printf("my_strlen(\"%s\") = %d\n", s1, (int) my_strlen(s1));
    printf("my_strlen(\"%s\") = %d\n", s2, (int) my_strlen(s2));
    printf("my_strlen(\"%s\") = %d\n", s3, (int) my_strlen(s3));
    printf("\n");

    // -- my_countchar, should be: 3, 0, and 2
    printf("my_countchar(\"%s\", 'l') = %d\n", s1, (int) my_countchar(s1, 'l'));
    printf("my_countchar(\"%s\", 'o') = %d\n", s2, (int) my_countchar(s2, 'o'));
    printf("my_countchar(\"%s\", 'o') = %d\n", s3, (int) my_countchar(s3, 'o'));
    printf("\n");
    
    // -- my_strchr, should be: "llo World!", "(null)", and ""
    printf("my_strchr(\"%s\", 'l') = %s\n", s1, my_strchr(s1, 'l'));
    printf("my_strchr(\"%s\", 'o') = %s\n", s2, my_strchr(s2, 'o'));
    printf("my_strchr(\"%s\", '\\0') = %s\n", s3, my_strchr(s3, '\0'));
    printf("\n");

    // -- my_strrchr, should be: "ld!", "(null)", and ""
    printf("my_strrchr(\"%s\", 'l') = %s\n", s1, my_strrchr(s1, 'l'));
    printf("my_strrchr(\"%s\", 'o') = %s\n", s2, my_strrchr(s2, 'o'));
    printf("my_strrchr(\"%s\", '\\0') = %s\n", s3, my_strrchr(s3, '\0'));
    printf("my_strrchr(\"\", '\\0') = %s\n", my_strrchr("", '\0'));
    printf("my_strrchr(\"Find First 'F' from right\", 'F') = %s\n", my_strrchr("Find First 'F' from right", 'F'));
    printf("my_strrchr(\"Find first char\", 'F') = %s\n", my_strrchr("Find first char", 'F'));
    printf("my_strrchr(\"!Find last char!\", '!') = %s\n", my_strrchr("!Find last char!", '!'));
    printf("\n");

    // -- my_strstr, should be: "World!", "Hello World!", "(null)"
    printf("my_strstr(\"%s\", \"World\") = %s\n", s1, my_strstr(s1, "World"));
    printf("my_strstr(\"%s\", \"\") = %s\n", s1, my_strstr(s1, ""));
    printf("my_strstr(\"%s\", \"hello\") = %s\n", s1, my_strstr(s1, "hello"));
    printf("\n");

    // -- my_strcpy. For this function you need a buffer where you
    // copy the string to. 
    char buffer[BUFFER_LEN];
    my_strcpy(buffer, s1);
    printf("my_strcpy(buffer, \"%s\"), buffer = \"%s\"\n", s1, buffer);
    my_strcpy(buffer, s2);
    printf("my_strcpy(buffer, \"%s\"), buffer = \"%s\"\n", s2, buffer);
    my_strcpy(buffer, s3);
    printf("my_strcpy(buffer, \"%s\"), buffer = \"%s\"\n", s3, buffer);
    printf("\n");

    // -- my_strcat. You will have to do this yourself... just
    // look at my_strcpy for an example, and go from there.
    char dest[BUFFER_LEN];
    char src[BUFFER_LEN];
    my_strcpy(dest, "Hello ");
    my_strcpy(src, "Zippy!");
    printf("my_strcat(\"Hello \", \"%s\") = \"%s\"\n", src,  my_strcat(dest, src));
    my_strcpy(dest, "");
    my_strcpy(src, "Hi!");
    printf("my_strcat(\"\", \"%s\") = \"%s\"\n", src,  my_strcat(dest, src));
    printf("\n");
    
    // -- my_isspace. You will have to do this for yourself.
    printf("my_isspace(' ') = %d\n", my_isspace(' '));
    printf("my_isspace('\\t') = %d\n", my_isspace('\t'));
    printf("my_isspace('\\v') = %d\n", my_isspace('\v'));
    printf("my_isspace('\\f') = %d\n", my_isspace('\f'));
    printf("my_isspace('\\n') = %d\n", my_isspace('\n'));
    printf("my_isspace('\\r') = %d\n", my_isspace('\r'));
    printf("\n");

    // -- my_atoi. You will have to do this for yourself.
    printf("my_atoi(\"%s\") = %d\n", s1, my_atoi(s1));
    printf("my_atoi(\"32 students got an F\") = %d\n", my_atoi("32 students got an F"));
    printf("my_atoi(\"\\n\\t\\v -273\") = %d\n", my_atoi("\n\t\v -273"));
    printf("\n");
    return EXIT_SUCCESS;
}
Esempio n. 3
0
int main(int argc, char * * argv)
{
    printf("Welcome to PA02.\n"
	   "\n"
	   "You are encouraged to edit this file in order to test\n"
	   "the behavior of the functions you write in answer02.c\n"
	   "\n"
	   "This file will not be marked, and should not be\n"
	   "submitted.\n"
	   "\n"
	   "Don't forget to post questions on blackboard, and ask\n"
	   "the TAs and your classmates for help.\n"
	   "\n");

    const char * s1 = "Hello World!";
    const char * s2 = "";
    const char * s3 = "foo";

    // -- my_strlen, should be: 12, 0, and 3
    printf("my_strlen(\"%s\") = %d\n", s1, (int) my_strlen(s1));
    printf("my_strlen(\"%s\") = %d\n", s2, (int) my_strlen(s2));
    printf("my_strlen(\"%s\") = %d\n", s3, (int) my_strlen(s3));

    // -- my_countchar, should be: 3, 0, and 2
    printf("my_countchar(\"%s\", 'l') = %d\n", s1, (int) my_countchar(s1, 'l'));
    printf("my_countchar(\"%s\", 'o') = %d\n", s2, (int) my_countchar(s2, 'o'));
    printf("my_countchar(\"%s\", 'o') = %d\n", s3, (int) my_countchar(s3, 'o'));

    // -- my_strchr, should be: "llo World!", "(null)", and ""
    printf("my_strchr(\"%s\", 'l') = %s\n", s1, my_strchr(s1, 'l'));
    printf("my_strchr(\"%s\", 'o') = %s\n", s2, my_strchr(s2, 'o'));
    printf("my_strchr(\"%s\", '\\0') = %s\n", s3, my_strchr(s3, '\0'));

    // -- my_strrchr, should be: "lo World!", "(null)", and ""
    printf("my_strrchr(\"%s\", 'o') = %s\n", s1, my_strrchr(s1, 'o'));
    printf("my_strrchr(\"%s\", 'z') = %s\n", s1, my_strrchr(s1, 'z'));
    printf("my_strrchr(\"%s\", '\\0') = %s\n", s1, my_strrchr(s1, '\0'));

    // -- my_strstr, should be: "World!", "Hello World!", "(null)"
    printf("my_strstr(\"%s\", \"World\") = %s\n", s1, my_strstr(s1, "World"));
    printf("my_strstr(\"%s\", \"\") = %s\n", s1, my_strstr(s1, ""));
    printf("my_strstr(\"%s\", \"hello\") = %s\n", s1, my_strstr(s1, "hello"));

    // -- my_strcpy. For this function you need a buffer where you
    // copy the string to. 
    char buffer[BUFFER_LEN];
    my_strcpy(buffer, s1);
    printf("my_strcpy(buffer, \"%s\"), buffer = \"%s\"\n", s1, buffer);
    my_strcpy(buffer, s2);
    printf("my_strcpy(buffer, \"%s\"), buffer = \"%s\"\n", s2, buffer);
    my_strcpy(buffer, s3);
    printf("my_strcpy(buffer, \"%s\"), buffer = \"%s\"\n", s3, buffer);

    // -- my_strcat. You will have to do this yourself... just
    // look at my_strcpy for an example, and go from there.
    my_strcpy(buffer, "Hello ");
    printf("%s\n", my_strcat(buffer, "Zippy!")); // prints "Hello Zippy!"
    my_strcpy(buffer, "");
    printf("%s\n", my_strcat(buffer, "Hello!")); // prints ("Hello!")   

    // -- my_isspace. You will have to do this for yourself.
    printf("my_isspace('\\n') = %d\n", my_isspace(' '));
    printf("my_isspace('\\f') = %d\n", my_isspace('\f'));
    printf("my_isspace('\\r') = %d\n", my_isspace('\r'));
    printf("my_isspace('\\t') = %d\n", my_isspace('\t'));
    printf("my_isspace('n') = %d\n", my_isspace('l'));

    // -- my_atoi. You will have to do this for yourself.
    printf("my_atoi(\"0\") = %d\n", my_atoi("0")); // 0
    printf("my_atoi(\"-12\") = %d\n", my_atoi("-12")); // -12
    printf("my_atoi(\"15th of March would be the ides.\") = %d\n", my_atoi("15th of March would be the ides.")); // 15
    printf("my_atoi(\"4 months to Summer.\") = %d\n", my_atoi("4 months to Summer.")); // 4
    printf("my_atoi(\"\\n\\f\\t\\v\\r 6 white space characters handled correctly.\") = %d\n", my_atoi("\n\f\t\v\r 6 white space characters handled correctly.")); // 6
    printf("my_atoi(\"garbage should yield 0\") = %d\n", my_atoi("garbage should yield 0")); // 0

    return EXIT_SUCCESS;
}
Esempio n. 4
0
int main(int argc, char *argv[])
{
   // if(testme())
     //   return 0;
    if (argc == 2 && (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0)) 
	{
	    printUsage(argv[0]);
	    return EXIT_SUCCESS;
	}

    if (argc != 3) {
	printUsage(argv[0]);
	return EXIT_FAILURE;
    }

    FILE * f;
    if ((f = fopen(argv[2], "r")) == NULL) {
	printf("Unable to open file '%s', aborting!\n", argv[2]);
	return EXIT_FAILURE;
    }
    FILE * fptrout = stdout;

    int line_count = 0;
    char line_buffer[LINE_SIZE];
    while (fgets(line_buffer, LINE_SIZE, f) != NULL) 
	{
	    line_count++;
	}
    int file_length = ftell(f);
    rewind(f);
    
    char * * src = malloc(sizeof(char *) * line_count);
    char * * copy = malloc(sizeof(char *) * line_count);
    int i;
    for (i = 0; i < line_count; i++) 
	{
	    if (feof(f)) 
		{
		    printf("Not enough lines in file!\n");
		    fclose(f);
		    return EXIT_FAILURE;
		}
	    copy[i] = malloc(sizeof(char) * LINE_SIZE);
	    fgets(copy[i], LINE_SIZE, f);
	    trim(copy[i]);
	    src[i] = strdup(copy[i]);
	}
    fclose(f);

    char * buffer = malloc(sizeof(char) * file_length);
    buffer[0] = '\0';
    //Partitioning outputs
    const char * command = argv[1];

    if (strcmp(command, "my_strlen") == 0) {
	for (i = 0; i < line_count; i++) {
	    fprintf(fptrout, "length: %d\n", my_strlen(copy[i]));
	}
    } else if (strcmp(command, "my_countchar") == 0) {
	for (i = 0; i < line_count; i++) {
	    fprintf(fptrout, "count(%c): %d\n", copy[i][0], my_countchar(copy[i], copy[i][0]));
	}
    } else if (strcmp(command, "my_strupper") == 0) {
	for (i = 0; i < line_count; i++) {
	    my_strupper(copy[i]);
	    fprintf(fptrout, "uppercase: %s\n", copy[i]);
	}
    } else if (strcmp(command, "my_strlower") == 0) {
	for (i = 0; i < line_count; i++) {
	    my_strlower(copy[i]);
	    fprintf(fptrout, "lowercase: %s\n", copy[i]);
	}
    } else if (strcmp(command, "my_strcat") == 0) {
	for (i = 0; i < line_count; i++) {
	    my_strcat(copy[i], " ");
	    my_strcat(copy[i], src[i]);
	    fprintf(fptrout, "%s\n", copy[i]);
	}
    } else if (strcmp(command, "my_strncat") == 0) {
	for (i = 0; i < line_count; i++) {
	    my_strncat(copy[i], "   ", 2);
	    my_strncat(copy[i], "........", 1);
	    fprintf(fptrout, "%s\n", copy[i]);
	}
    } else if (strcmp(command, "my_strcpy") == 0) {
	for (i = 0; i < line_count; i++) {
	    my_strcpy(copy[i], "Copying this String.");
	    fprintf(fptrout, "%s\n", copy[i]);
	}
    } else if (strcmp(command, "my_strncpy") == 0) {
	for (i = 0; i < line_count; i++) {
	    my_strncpy(copy[i], src[i], 5);
	    fprintf(fptrout, "%s\n", copy[i]);
	}
    } else if (strcmp(command, "my_strstr") == 0) {
	for (i = 0; i < line_count; i++) {
	    fprintf(fptrout, "%d\n", my_strstr(copy[i], src[i]) != NULL);
	}
    } else if (strcmp(command, "my_strinsert") == 0) {
	for (i = 0; i < line_count; i++) {
	    my_strinsert(buffer, "\n", 0);
	    my_strinsert(buffer, src[i], 0);
	}
	fprintf(fptrout, "[%s]\n", buffer);
    } else if (strcmp(command, "my_strdelete") == 0) {
	for (i = 0; i < line_count; i++) {
	    my_strinsert(buffer, "\n", 0);
	    my_strinsert(buffer, src[i], 0);
	    my_strdelete(buffer, 0, 10);
	}
	fprintf(fptrout, "[%s]\n", buffer);
    }

    for (i = 0; i < line_count; i++) {
	free(copy[i]);
	free(src[i]);
    }
    free(src);
    free(copy);
    free(buffer);
    fclose(fptrout);
    return EXIT_SUCCESS;
}