Hi,
I have to write a code which would determine either a URL address
is correct or not.
Now, a valid address should look like: "www.something.something.uk".
It has to have 3 dots, 3 w-s in the beginning, and it must end with "uk".
eg.
Valid addresses:
1. www.jce.ac.uk
2. www.tr2213.oi34.uk
Invalid addresses:
1. www2.jce.ac.uk
2. עזריאלי - מכללה אקדמית להנדסה ירושלים - לימודי הנדסה לתואר ראשון
3. www.something.uk
Just to be clear, this criteria mentioned above is not real, just homework
My code:
Code:#include <iostream> #include <string.h> using namespace std; int isValid (char s[]) { int dots=0; if ((s[0]!='w') || (s[1]!='w') || (s[2]!='w') || (s[3]!='.')) return 0; else { dots++; if (s[4]=='.') return 0; else { for (int i=5;i<(strlen(s));i++) { if (s[i]=='.') dots++; if ((s[i]=='.')&&(s[i+1]=='.')) return 0; } } } if ((s[(strlen(s)-1)]=='k') && (s[(strlen(s)-2)]!='u') && (dots==3)) return 1; else return 0; } void main() { char string1[14]="www.jce.ac.uk", string2[18]="www.dsa.dsa.gt.uk"; if (isValid(string1)) cout << "string1 - correct\n"; else cout << "string1 - incorrect\n"; if (isValid(string2)) cout << "string2 - correct\n"; else cout << "string2 - incorrect\n"; }
It tells me both strings are incorrect but the first 1 is.
Thanks for the help!