Alphabetical comparison of two strings in C -
my programming experience extremely scattered , can't seem find answer hope simple question. had assignment last week didn't finish , it's bugging me death.
i supposed compare 2 strings alphabetically without using strcmp , find out string alphabetically first via function using pointers.
int strcmp373(char *str1, char *str2) { while(*str1 != '\0') { str1++; } while(*str2 != '\0') { str2++; } if(*str1 == *str2) } this horrible attempt, thought process being use null terminated value. hoping insight , explain how works?
here's copy of assignment specifications reference.
write function called strcmp373, compares 2 strings in precisely same way strcmp in c library . time, please use "pointer syntax" in writing function. is, [ ] operator should not used @ when referring particular characters in string1 , string2; instead, parameters , local variables should declared pointers (using * symbol). please sure emulate strcmp c function. note strcmp returns 0 if 2 strings equal, though 0 means false in c. sign of other return values matters used indicate in way strings not same, precise return value not important. may not use of built-in c string library functions complete code.
here prototype of function:
int strcmp373(char *, char *); and here main function can use test strcmp373.
#include <stdio.h> #include "hw3.h" // discussed int main() { char str1[81], str2[81]; char again = 'y', newline; while (again == 'y') { printf("enter string\n"); scanf("%s", str1); printf("enter string\n"); scanf("%s", str2); int comp = strcmp373(str1, str2); if (comp < 0) printf("%s alphabetically before %s\n", str1, str2); else if (comp > 0) printf("%s alphabetically after %s\n", str1, str2); else printf("%s , %s same\n", str1, str2); printf("again? (y/n)\n"); scanf("%c%c", &newline, &again); } }
let's str1 points holds "abcd" , str2 points holds "abc".
str1 | v +---+---+---+---+----+ | | b | c | d | \0 | +---+---+---+---+----+ str2 | v +---+---+---+----+ | | b | c | \0 | +---+---+---+----+ when execute
while(*str1 != '\0') { str1++; } you move str1 until points null character.
str1 | v +---+---+---+---+----+ | | b | c | d | \0 | +---+---+---+---+----+ str1 | v +---+---+---+---+----+ | | b | c | d | \0 | +---+---+---+---+----+ str1 | v +---+---+---+---+----+ | | b | c | d | \0 | +---+---+---+---+----+ str1 | v +---+---+---+---+----+ | | b | c | d | \0 | +---+---+---+---+----+ str1 | v +---+---+---+---+----+ | | b | c | d | \0 | +---+---+---+---+----+ similarly, when execute
while(*str2 != '\0') { str2++; } you move str2 until points null character.
str2 | v +---+---+---+----+ | | b | c | \0 | +---+---+---+----+ str2 | v +---+---+---+----+ | | b | c | \0 | +---+---+---+----+ str2 | v +---+---+---+----+ | | b | c | \0 | +---+---+---+----+ str2 | v +---+---+---+----+ | | b | c | \0 | +---+---+---+----+ when while loops done, both str1 , str2 point null character. hence, *str1 == *str2 evaluates true.
what need compare *str1 , *str2 , increment them if equal until not equal or reach end of strings.
str1 | v +---+---+---+---+----+ | | b | c | d | \0 | +---+---+---+---+----+ str2 | v +---+---+---+----+ | | b | c | \0 | +---+---+---+----+ str1 | v +---+---+---+---+----+ | | b | c | d | \0 | +---+---+---+---+----+ str2 | v +---+---+---+----+ | | b | c | \0 | +---+---+---+----+ str1 | v +---+---+---+---+----+ | | b | c | d | \0 | +---+---+---+---+----+ str2 | v +---+---+---+----+ | | b | c | \0 | +---+---+---+----+ str1 | v +---+---+---+---+----+ | | b | c | d | \0 | +---+---+---+---+----+ str2 | v +---+---+---+----+ | | b | c | \0 | +---+---+---+----+ now know not equal , 'd' greater tha '\0' return positive value indicating lhs alphabetically greater rhs.
that logic can implemented using:
while ( *str1 != '\0' && *str1 == *str2 ) { ++str1; ++str2; } return (*str1 - *str2);
Comments
Post a Comment