<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Puzzle</title>
</head>

<body>

<p align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#FF0000" size="6"><b> 
</b></font><font color="#FF0000" size="6"><b>Little function</b></font>     
</p>
<div align="left">
  <pre><b><font color="#ff0000" size="5">A.First Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is the first edition of this simple function to delete first and last non-alphabetic char of a string.</b></pre>
</div>
<div align="left">
  <pre><b>1¡£ Basic idea: </b></pre>
</div>
<div align="left">
  <pre><b>2¡£ Program design: </b></pre>
</div>
<div align="left">
  <pre><b>3¡£ Major function£º</b></pre>
</div>
<div style="WIDTH: 892px; HEIGHT: 102px" align="left">
  <pre><b>     A.  </b></pre>
  <pre><b>4¡£ Further improvement£º</b></pre>
  <pre><b>	A. I write it for fun with a couple of hours and  I am not interested in improving it.</b></pre>
  <pre><b>	</b></pre>
</div>
<div align="left">
  <pre><b>		</b></pre>
</div>
<pre>#include &lt;iostream&gt;</pre>
<pre>using namespace std;
</pre>
<pre>void removeMark(char* word);</pre>
<pre>void arrange(char* dictionary, const int arraySize, int wordSize);</pre>
<pre>//the testing data, make sure the total char including '\0' not exceeding the max length of 
//word length; otherwise you will make two word as one long word,
char* str[5] = {&quot;$firsts&quot;, &quot;$secon&amp;&quot;, &quot;&amp;fully&amp;&quot;, &quot;normal&amp;&quot;, &quot;w*&quot;};</pre>
<pre>int main()
{
	//this is a little test to show you the effect
	char dictionary[40];// this is your big array with 5 word, each 8bytes </pre>
<pre>	//I now fill in with 5 word with punches
	for (int i =0; i&lt;5; i++)
	{
		//use address of first char as a string to accept the words
		strcpy(&amp;dictionary[i*8], str[i]);
	}</pre>
<pre>	//show you the word before &quot;arrange&quot;
	cout&lt;&lt;&quot;\nNow is the word before arrange\n&quot;;
	for (int j =0; j&lt;5; j++)
	{
		char* ptr;
		ptr = &amp;dictionary[j*8];
		cout&lt;&lt;ptr&lt;&lt;endl;
	}</pre>
<pre>	//arrange with 5words, each 8bytes
	arrange(dictionary, 5, 8);</pre>
<pre>	//show you the effect after arrange
	cout&lt;&lt;&quot;\nNow I show you the effect after arrange\n&quot;;
	for (j =0; j&lt;5; j++)
	{
		char* ptr;
		ptr = &amp;dictionary[j*8];
		cout&lt;&lt;ptr&lt;&lt;endl;
	}
</pre>
<pre>	return 0;
}</pre>
<pre>void removeMark(char* word)
{
	char * ptrFirst, *ptrSecond;</pre>
<pre>	char ch;
	ch = word[0];  //I have to store the first char as it will be changing after copy
	ptrFirst = word;
	ptrSecond = ptrFirst +1;</pre>
<pre>	while (*ptrFirst!= '\0')
	{	
		//you need to seperate two situation:
		//this is the first char is not a alphabetic
		if (!isalpha(ch))
		{	
			//you copy each char from next to previous
			*ptrFirst = *ptrSecond;</pre>
<pre>			//and move to next char
			ptrFirst = ptrSecond;
			ptrSecond = ptrFirst + 1;</pre>
<pre>			//until you find out the last char is also not alphabetic
			//unless you notice the ptrSecond is '\0'
			if (*ptrSecond=='\0'&amp;&amp;(!isalpha(*ptrFirst)))
			{
				//it is the only way you should go back a position to write '\0' to 
				//over the non-alphabetic char
				ptrFirst--;
				*ptrFirst= '\0';
				break;
			}	
		}
		else
		{
			//this is the second situation that the first char is indeed alphabetic char
			ptrFirst ++;
			ptrSecond ++;
			//you just move to next without copy
			if (*ptrSecond=='\0'&amp;&amp;(!isalpha(*ptrFirst)))
			{
				//until you find the last char is non-alphabetic
				//you ten copy by over-writing '\0' to the non-alphabetic char
				*ptrFirst = *ptrSecond;
				break;
			}	
		}</pre>
<pre>	}
}

</pre>
<pre>//dictionary is your big char array
//arraysize is how many word you stored in dictionary
//wordsize is the max length of each word 
void arrange(char* dictionary, const int arraySize, int wordSize)
{
	void removeMark(char* word);
	</pre>
<pre>	for (int i=0; i&lt; arraySize; i++)
	{
		//actually you use the address of first char of word to make it as a string as paramenter
		removeMark(&amp;dictionary[i * wordSize]);
	}
}</pre>
<pre>¡¡</pre>
<pre>	<img border="0" src="picture/arrayTest.JPG" width="666" height="434">

</pre>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;               
&nbsp;&nbsp;&nbsp; <a href="detective.htm">         







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
<a href="wordReader.htm"><img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</a>          


</p>

</body>

</html>
