Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
int House_number(string address)

What I have tried:

C++
int HOUSE_NUMBER(string address)
{
	string number;
	
	for (int i = 0; i < address[i]; i++)
	{
		if (true)
		{

		}
	}
Posted
Updated 12-Jul-21 1:58am
v2
Comments
Richard Deeming 12-Jul-21 7:05am    
Depends what your definition of "house number" is. For example, your method will not cope with "221b Baker Street" - the house number of "221b" cannot be returned as an integer.

C++
for (int i = 0; i < address[i]; i++)
{
    if (address[i] >= '0' and address[i] <= '9')
    {
        // it is a number
    }
}

However there are generally rules in addresses as to where a house number may appear. Here in the UK it is usually the first field of the address. In Turkey (at least it was in the seventies) it was the last field of the first line. So you need to start by setting your rules.
 
Share this answer
 
You will need to look at or enforce some kind of structure in your address string - because without that, you can't reliably automatically process it.

For example, how do you pick the house number out of this:
Flat 12, 23 Albert row, London L17 8DS
And also get the house number from this:
Helton House, 1 Laung Street, Bananatown, Hants GU32 8RT
And this:
Number One, LondonAll of which are valid UK addresses!

Without structure and a defined input format, your code will make more mistakes than correct extractions!
 
Share this answer
 
Comments
Filip Picek 12-Jul-21 7:55am    
is there any rule when i want to find something how to find it? for example i want to find a number in a vector do i search the same as when i want to find a number in a string?
C++
#include <regex>
#include <iterator>
#include <iostream>
#include <string>

using namespace std;

int House_number(string address) // warning, this may throw
{
  regex number_regex("[\\d]+");
  auto number_begin = sregex_iterator(address.begin(), address.end(), number_regex);

  return stoi( number_begin->str());
}



int main()
{
  const string sh = "Where is 221b Baker Street";
  const string tc = "Where is the cat?";
  try
  {
    cout << House_number(sh) << endl;
    cout << House_number(tc) << endl;
  }
  catch (exception & e)
  {
    cerr << "the following exception occurred: " << e.what() << endl;
  }
}

Anyway, as Richard correctly pointed out, this way you're not able to find Sherlock Holmes.
 
Share this answer
 
Comments
Richard MacCutchan 12-Jul-21 7:59am    
As OriginalGriff points out, you won't find the Duke of Wellington's house either.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900