Reputation: 17
My programming experience is extremely scattered and I can't seem to find the answer to what I hope is a simple question. I had an assignment last week that I didn't properly finish and it's bugging me to death.
I was supposed to compare two strings alphabetically without using strcmp and find out which string is alphabetically first via a function using pointers.
int strcmp373(char *str1, char *str2) {
while(*str1 != '\0')
{
str1++;
}
while(*str2 != '\0')
{
str2++;
}
if(*str1 == *str2)
}
This is my horrible attempt, my thought process being to use the null terminated value. I was hoping I could get some insight and explain how it works?
Here's a copy of the assignment specifications for reference.
Write a function called strcmp373, which compares two strings in precisely the same way that strcmp does in the C library . This time, please use "pointer syntax" in writing this function. That is, the [ ] operator should not be used at all when referring to particular characters in string1 and string2; instead, all parameters and local variables should be declared as pointers (using the * symbol). Please be sure that you emulate the strcmp C function. Note that strcmp returns 0 if the two strings are equal, even though 0 normally means false in C. The sign of other return values matters is used to indicate in what way the strings are not the same, but the precise return value is not important. You may not use any of the built-in C string library functions to complete this code.
Here is the prototype of this function:
int strcmp373(char *, char *);
And here is a main function which you can use to test strcmp373.
#include <stdio.h>
#include "hw3.h" // to be discussed
int main() {
char str1[81], str2[81];
char again = 'y', newline;
while (again == 'y') {
printf("Enter a string\n");
scanf("%s", str1);
printf("Enter another string\n");
scanf("%s", str2);
int comp = strcmp373(str1, str2);
if (comp < 0)
printf("%s is alphabetically before %s\n", str1, str2);
else if (comp > 0)
printf("%s is alphabetically after %s\n", str1, str2);
else printf("%s and %s are the same\n", str1, str2);
printf("Again? (y/n)\n");
scanf("%c%c", &newline, &again);
}
}
Upvotes: 0
Views: 12210
Reputation: 206667
Let's say str1
points to something that holds "abcd" and str2
points to something that holds "abc".
str1
|
v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+
str2
|
v
+---+---+---+----+
| a | b | c | \0 |
+---+---+---+----+
When you execute
while(*str1 != '\0')
{
str1++;
}
You move str1
until it points to the null character.
str1
|
v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+
str1
|
v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+
str1
|
v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+
str1
|
v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+
str1
|
v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+
Similarly, when you execute
while(*str2 != '\0')
{
str2++;
}
You move str2
until it points to the null character.
str2
|
v
+---+---+---+----+
| a | b | c | \0 |
+---+---+---+----+
str2
|
v
+---+---+---+----+
| a | b | c | \0 |
+---+---+---+----+
str2
|
v
+---+---+---+----+
| a | b | c | \0 |
+---+---+---+----+
str2
|
v
+---+---+---+----+
| a | b | c | \0 |
+---+---+---+----+
When the while
loops are done, both str1
and str2
point to a null character. Hence, *str1 == *str2
always evaluates to true
.
What you need is to compare *str1
and *str2
and then increment them together if they are equal until they are not equal or you reach the end of the strings.
str1
|
v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+
str2
|
v
+---+---+---+----+
| a | b | c | \0 |
+---+---+---+----+
str1
|
v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+
str2
|
v
+---+---+---+----+
| a | b | c | \0 |
+---+---+---+----+
str1
|
v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+
str2
|
v
+---+---+---+----+
| a | b | c | \0 |
+---+---+---+----+
str1
|
v
+---+---+---+---+----+
| a | b | c | d | \0 |
+---+---+---+---+----+
str2
|
v
+---+---+---+----+
| a | b | c | \0 |
+---+---+---+----+
Now that you know they are not equal and 'd'
is greater tha '\0'
return a positive value indicating the LHS is alphabetically greater than the RHS.
That logic can be implemented using:
while ( *str1 != '\0' && *str1 == *str2 )
{
++str1;
++str2;
}
return (*str1 - *str2);
Upvotes: 8
Reputation: 306
if(*str1 == *str2)
You have an if statement with no body in your code excerpt.
Apart from that your program is counting the amount of characters in the character array passed to that function. This won't really achieve your goal.
Lets call each char array a word. What you want to do is find which 'word' comes first in the alphabet. I suggest looking at an ASCII table.
Each letter in either word has a corresponding ASCII number value. If you can figure out how to access the number value of each of these letters, you can compare the strings and the lower value will be higher up in the alphabet.
Unless they are capitalized. That will require a little more work. Here is an ASCII table where you can see the difference.
This will get you started with figuring out how to get the ASCII value of these letters
Upvotes: 0