Thread: Simple valid/invalid function problem.

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    20

    Simple valid/invalid function problem.

    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!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    35,870
    Are you using C or C++?
    If this is C, then you need to stop using cout to print things.

    If this is C++, then you should use std::string instead of char arrays.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    20
    Oh I'm sorry, didnt know that.
    I'm using C, why should I stop using cout?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    because cout is not available in C - it is provided as part of C++ language

    You also should remove <iostream> and using namespace - they also are part of C++ only
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    20
    Okay toda
    Saw you are from Rashlatz, Im from Jerusalem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. valid function prototypes?
    By budafeet57 in forum C++ Programming
    Replies: 8
    Last Post: 03-12-2013, 06:47 AM
  2. Socket file descriptor valid and then not valid
    By Florian in forum C Programming
    Replies: 3
    Last Post: 05-22-2010, 08:23 AM
  3. A simple yet a valid question...
    By chottachatri in forum C Programming
    Replies: 13
    Last Post: 10-18-2008, 02:07 AM
  4. valid call to the function
    By kellymart87 in forum C++ Programming
    Replies: 3
    Last Post: 04-17-2007, 04:13 PM