Creating own strcmp and strcmpi
Oct 3, 2013 at 2:47pm UTC
We are asked to make our own function for strcmp and strcmpi. This is probably wrong, I'm quite clueless about this. All I know is that if the two strings are equal, it will return 0 value and so on.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
#include<iostream>
using namespace std;
int ownstrcmp (char str1[], char str2[])
{
if (str1 == str2)
{
return 0;
}
else if (str1 > str2)
{
return +1;
}
else
{
return -1;
}
}
int ownstrcmpi (char str1[], char str2[])
{
if (str1 == str2)
{
return 0;
}
else
{
return 1;
}
}
void main()
{
char username[100], password[100], uName[100], pWord[100];
int choice;
do
{
cout<<"Students Account\n1) Create\n2) Log-In\n3) Exit" <<endl<<endl;
cout<<"Choose: " ;
cin>>choice;
flushall();
switch (choice)
{
case 1:
int checkUsername;
do
{
checkUsername = 1;
cout<<"Enter UserName: " ;
gets(username);
int len = strlen(username);
int i = 0;
while (i < len)
{
if (!isalnum(username[i]))
{
checkUsername = 0;
break ;
}
i++;
}
}while (checkUsername == 0);
int checkPassword;
do
{
checkPassword = 1;
cout<<"Enter Password: " ;
gets(password);
int len = strlen(password);
int i = 0;
while (i < len)
{
if (isspace(password[i]))
{
checkPassword = 0;
break ;
}
i++;
}
}while (checkPassword == 0);
break ;
case 2:
cout<<"Username: " ;
gets(uName);
flushall();
cout<<"Password: " ;
gets(pWord);
flushall();
if (ownstrcmp(pWord,password) == 0 && ownstrcmpi(uName,username) == 0)
{
cout<<"Access Allowed" <<endl;
}
else
{
cout<<"Invalid username or password" <<endl;
}
break ;
case 3:
cout<<"Program Terminated" <<endl;
break ;
}
}while (choice != 3);
cout<<endl;
system("Pause" );
}
Oct 3, 2013 at 2:59pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
#include<iostream>
using namespace std;
int ownstrcmp (char str1[], char str2[])
{
char x = 0;
if (str1[x] == str2[x])
{
return 0;
}
else if (str1[x] > str2[x])
{
return +1;
}
else
{
return -1;
}
}
int ownstrcmpi (char str1[], char str2[])
{
int x = 0;
if (str1[x] == str2[x])
{
return 0;
}
else
{
return +1;
}
}
void main()
{
char username[100], password[100], uName[100], pWord[100];
int choice;
do
{
cout<<"Students Account\n1) Create\n2) Log-In\n3) Exit" <<endl<<endl;
cout<<"Choose: " ;
cin>>choice;
flushall();
switch (choice)
{
case 1:
int checkUsername;
do
{
checkUsername = 1;
cout<<"Enter UserName: " ;
gets(username);
int len = strlen(username);
int i = 0;
while (i < len)
{
if (!isalnum(username[i]))
{
checkUsername = 0;
break ;
}
i++;
}
}while (checkUsername == 0);
int checkPassword;
do
{
checkPassword = 1;
cout<<"Enter Password: " ;
gets(password);
int len = strlen(password);
int i = 0;
while (i < len)
{
if (isspace(password[i]))
{
checkPassword = 0;
break ;
}
i++;
}
}while (checkPassword == 0);
break ;
case 2:
cout<<"Username: " ;
gets(uName);
flushall();
cout<<"Password: " ;
gets(pWord);
flushall();
if ((ownstrcmp(pWord,password) == 0 && ownstrcmpi(uName,username) == 0))
{
cout<<"Access Allowed" <<endl;
}
else
{
cout<<"Invalid username or password" <<endl;
}
break ;
case 3:
cout<<"Program Terminated" <<endl;
break ;
}
}while (choice != 3);
cout<<endl;
system("Pause" );
}
Updated version. Still, the strcmp is not case-sensitive.>.>
Topic archived. No new replies allowed.