Hello everyone.
The only solution I can figure to solve my problem is to create an array of 10 character pointers.
I need to split up this domain: cse.psu.edu into 2 sub domains, cse and psu
and also into a top level domain, which is edu.
The max number of sub-domains allowed is 10
and the max length of each sub domain is 65.
So i figured somthing like char subDomain[10][65]
but now i can't figure how to store the domains in it and then check validation of each character.
I was thinking somthing liek:
[subdomain1]['\0']
[subdomain2][\0']
[....]
[subdomain10]['\0'];
I'm going to be reading files from a text file so the e-mails could be all of the following:
I already created a function that will parse the e-mails into a local part and a domain part.
Well currently my utlitiy functions only work for 1-dim character arrays, like my indexOf and GetLength, as well as my CopyTo functions...
Here is all my code:
Code:
bool IsSubDomainsValid(char *domain, char *subDomain, char *tld)
{
// Make sure it contains at least one '.'
if(IndexOf(subDomain,'.',0) == -1)
return false;
int subdomainCount = 0;
int startPos = 0;
//SubDomain[10][64];
//0 < Length < 64
//Must not begin or end with ‘-’
//extract sub domains from domain
do
{
int nextPos = IndexOf(domain,'.', startPos);
if ( nextPos != -1 )
CopyTo(startPos + 1, nextPos - startPos, domain, subDomain);
else
CopyTo(startPos + 1, nextPos - startPos, domain, tld);
startPos = nextPos + 1;
} while ( startPos != 0 );
return true;
}
That is the function i'm writing to extract the sub domains from the domain. but you can see in this funciton call:
Code:
CopyTo(startPos + 1, nextPos - startPos, domain, tld);
domain is only a 1-dim char array, char * but this won't work in my case.
Here are my utlitiy functions:
Code:
#include "stdafx.h"
#include "util.h"
int GetLength(const char *text, int startPos)
{
int size = 0;
while( text[startPos] != '\0' )
{
size = size + 1;
++startPos;
}
return size;
}
int IndexOf(const char *text, char aChar, int startPosition)
{
for ( ; startPosition < GetLength(text); startPosition++ )
{
if(text[startPosition] == aChar)
return startPosition;
}
return -1; //no character found
}
void Copy(const char * source, char * destination, int length)
{
for( int i = 0; i < length; i++ )
{
destination[i] = source[i];
}
destination[length] = '\0';
}
/*void CopyTo(int startPosition, int length, const char *source, char *destination)
{
for(int i = 0; i < length; i++)
{
destination[i] = source[i];
}
destination[length] = '\0';
}*/
void CopyTo(int startPosition, int length, const char *source, char *destination)
{
int i = 0;
for(; i < length; i++)
{
destination[i] = source[startPosition++];
}
destination[i] = '\0';
}
void Print(char *source)
{
for( int i = 0; i < GetLength(source); i++ )
{
std::cout << source[i];
}
std::cout << std::endl;
}
So what I need to do is modify my IndexOf function and CopyTo to handle a 2 dimensional character array i believe.... but everything i do isn't working...
Code:
void CopyTo(int startPosition, int length, const char *source, char **destination)
{
int i = 0;
int j = 0;
for(; i < length; i++)
{
for(int j < length; j++)
destination[i][j] = source[startPosition++];
}
destination[i][j] = '\0';
}
Can anyone think of a better way at handling this?
The reason I have to extract the sub domains and top level domains is becuase i hve to run the following checks on each sub domain and tld.
Make sure length < 7 and > 0
Make sure sub domains don't begin or end with a '-' character
I can't find any good tutorials that explain how to cycle threw a 2-dim character array and look at each element, if anyone knows of one and doesn't want to explain i'd be happy to read it.
thanks!