Simple valid/invalid function problem.

This is a discussion on Simple valid/invalid function problem. within the C Programming forums, part of the General Programming Boards category; Hi, I have to write a code which would determine either a URL address is correct or not. Now, a ...

  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
    32,855
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 07:47 AM
  2. Socket file descriptor valid and then not valid
    By Florian in forum C Programming
    Replies: 3
    Last Post: 05-22-2010, 09:23 AM
  3. A simple yet a valid question...
    By chottachatri in forum C Programming
    Replies: 13
    Last Post: 10-18-2008, 03:07 AM
  4. valid call to the function
    By kellymart87 in forum C++ Programming
    Replies: 3
    Last Post: 04-17-2007, 05:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21