Let's start from the function stringCmpi
.
int stringCmpi (char *s1,char *s2)
{
int i=0,diff=0;
for(i=0; s1[i]!='\0'; i++)
{
if( toupper(s1[i])!=toupper(s2[i]) )
return 1;
}
return 0;
}
and let's assume that s1
is "A"
and s2
is "AB"
. In this case the function returns 0 because the loop will stop its iteration after comparing the first character 'A'
of the both string.
So this function is incorrect.
Now let's consider the function cheak
int cheak ()
{
int k;
int j=SIZE;
for(k=0;k<MAX;k++)
for(j=MAX;j>=k;j--)
{
if(1==stringCmpi(names[k],names[j]))
{
return 0;
}
return 1;
}
}
It seems that the identifier MAX means the number of strings in the array. In this case the index with the value MAX specifiers a non-existent string because the valid range of indices is [0, MAX)
. SO the function already has undefined behavior.
If two names are unequal you at once exit the function though the array can conatin equal names. So the function can return a wrong result.
What you need is the following.
#include <stdio.h>
#include <ctype.h>
int stringCmpi( const char *s1, const char *s2 )
{
while ( *s1 != '\0' &&
toupper( ( unsigned char )*s1 ) == toupper( ( unsigned char )*s2 ) )
{
++s1; ++s2;
}
return *s1 != *s2;
}
int check( size_t m, size_t n, char names[const m][n] )
{
int unique = 1;
for (size_t i = 0 ; unique && i < m; i++ )
{
for ( size_t j = i + 1; unique && j < m; j++ )
{
unique = stringCmpi( names[i], names[j] );
}
}
return unique;
}
int main(void)
{
{
enum { M = 3, N = 10 };
char names[M][N] = { "Bob", "Tome", "David" };
if( check( M, N, names ) )
{
printf( "\ngreat! the names are not repeat\n" );
}
else
{
printf( "\nyou repeat names\n" );
}
}
{
enum { M = 3, N = 10 };
char names[M][N] = { "Bob", "Tome", "Bob" };
if( check( M, N, names ) )
{
printf( "\ngreat! the names are not repeat\n" );
}
else
{
printf( "\nyou repeat names\n" );
}
}
return 0;
}
The program output is
great! the names are not repeat
you repeat names
Or if your compiler does not support variable length arrays then the program can look like
#include <stdio.h>
#include <ctype.h>
#define M 3
#define N 10
int stringCmpi( const char *s1, const char *s2 )
{
while ( *s1 != '\0' &&
toupper( ( unsigned char )*s1 ) == toupper( ( unsigned char )*s2 ) )
{
++s1; ++s2;
}
return *s1 != *s2;
}
int check( char names[][N], size_t m )
{
int unique = 1;
for (size_t i = 0 ; unique && i < m; i++ )
{
for ( size_t j = i + 1; unique && j < m; j++ )
{
unique = stringCmpi( names[i], names[j] );
}
}
return unique;
}
int main(void)
{
{
char names[M][N] = { "Bob", "Tome", "David" };
if( check( names, M ) )
{
printf( "\ngreat! the names are not repeat\n" );
}
else
{
printf( "\nyou repeat names\n" );
}
}
{
char names[M][N] = { "Bob", "Tome", "Bob" };
if( check( names, M ) )
{
printf( "\ngreat! the names are not repeat\n" );
}
else
{
printf( "\nyou repeat names\n" );
}
}
}