having some difficulty with some code around strncmp in C just wondering if there is someone that ran into the same problem has me
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int endsWith(char* longStr, char* shortStr);
int main()
{
char* longString = "Grant is The man";
char* shortString = "man";
endsWith(longString,shortString);
printf("%s\n",shortString);
}
int endsWith(char longStr, char shortStr)
{
int offset;
for (offset = 0 ; offset < strlen(shortStr) - strlen(longStr) ; offset )
if (strncmp( longStr , shortStr offset , strlen(longStr)) == 0)
return 1;
return -1;
}
the return should be man and if i inserted is the nothing should be returned or 0.
CodePudding user response:
You have several problems:
- There's no need for a loop. Just get the offset and compare from there once.
- You're calculating the offset backwards. You need to subtract the short length from the long length.
- There's no need to use
strncmp()
,strcmp()
will work. - A testing function should return
1
or0
, not1
or-1
. - You're never checking the result of the function.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int endsWith(char* longStr, char* shortStr);
int main()
{
char* longString = "Grant is The man";
char* shortString = "man";
if (endsWith(longString,shortString)) {
printf("%s\n",shortString);
}
}
int endsWith(char longStr, char shortStr)
{
int offset = strlen(longStr) - strlen(shortStr);
if (offset < 0) { // shortStr is longer than longStr
return 0;
}
return strcmp(shortStr, longStr offset) == 0;
}
CodePudding user response:
For starters the type of the function parameters in the function definition
int endsWith(char longStr, char shortStr)
does not correspond to the type in the previous function declaration.
At least you need to write in the function definition
int endsWith(char *longStr, char *shortStr)
But it would be more correct to declare the function like
int endsWith( const char *longStr, const char *shortStr );
because the passed strings are not changed within the function.
Your approach with the function strncmp
does not make a great sense.
Moreover this call
strncmp( longStr , shortStr offset , strlen(longStr))
is just incorrect. For example why is there used the expression shortStr offset
?
If the purpose of the function is to determine whether the first string is ended with the second string then the function can be defined the following way
int endsWith( const char *longStr, const char *shortStr );
{
size_t n1 = strlen( longStr );
size_t n2 = strlen( shortStr );
int success = -1;
if ( !( n1 < n2 ) )
{
if ( strcmp( longStr n1 - n2, shortStr ) == 0 ) success = 1;
}
return success;
}
Or if you want to return 1 in the case of success or 0 otherwise then the function definition can look like
int endsWith( const char *longStr, const char *shortStr );
{
size_t n1 = strlen( longStr );
size_t n2 = strlen( shortStr );
return !( n1 < n2 ) && strcmp( longStr n1 - n2, shortStr ) == 0;
}
CodePudding user response:
There are a number of problems with your code.
your declaration of
endsWith()
does not match its definition.main()
is ignoring the return value ofendsWith()
there is no need for a loop in
endsWith()
at all. Your approach is implementing anindexOf()
function rather than anendsWith()
function. You can calculate the neededoffset
with a single subtraction, no looping required.But, even if a loop were needed, you are not calculating the loop condition correctly. You are subtracting a larger value from a shorter value, which will result in a negative value. But since
strlen()
returns an unsigned type, a negative will wrap to a very large positive value, so you end up looping more than you need.you are passing the wrong parameters to
strncmp()
. Since you want to compare the end of thelongStr
to see if it matches theshortStr
, you need to passlongStr offset
instead ofshortStr offset
, and also passstrlen(shortStr)
instead ofstrlen(longStr)
.endsWith()
should return0
if a match is not found, not-1
.
With that said, try something more like this instead:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int endsWith(const char* longStr, const char* shortStr);
int main()
{
const char* longString = "Grant is The man";
const char* shortString = "man";
if (endsWith(longString, shortString))
printf("Ends with %s\n", shortString);
else
printf("Does not end with %s\n", shortString);
}
int endsWith(const char* longStr, const char* shortStr)
{
size_t longLen = strlen(longStr);
size_t shortLen = strlen(shortStr);
return (
(longLen >= shortLen) &&
(strncmp(longStr (longLen-shortLen), shortStr, shortLen) == 0)
) ? 1 : 0;
}