<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>lost in strings - C++ - tribe.net</title>
    <link>http://cpp.tribe.net/thread/11e02fd7-3a81-468a-b637-3620c29c12d5?format=rss</link>
    <description>Tribe.net. Local Connections</description>
    <item>
      <title>Re: lost in strings -- Correction to myself!</title>
      <link>http://CPP.tribe.net/thread/11e02fd7-3a81-468a-b637-3620c29c12d5#9399892f-0af4-44d9-aaab-4827cd283f51</link>
      <description>Ok, Actually, I tested and debugged this and found a critical error, my function is an infinite loop. You must feed the last index found +1 into the next iteration of the find_first_of function, (+1 is critical), or you keep refinding the same (first) matched character.&#xD;
&#xD;
This makes the starting index a bit annoying, because if you initialize your local index variable to 0, then "0+1" skips the first character and it never gets searched... So I initialized my search index to -1 before the loop, so the index+1 being fed into the function will evaluate to 0 the first iteration and whatever it finds + 1 on subsequent iterations...&#xD;
&#xD;
I have tested this function with a simple input string, searching for periods, spaces, several letters... spereately, together... etc... In short, it works..&#xD;
&#xD;
Here's the condensed version. No error check, no commnets, few locals... &#xD;
&#xD;
//---------------------------&#xD;
// Begin example.&#xD;
//---------------------------&#xD;
unsigned int CountChars(&#xD;
	const char* pStringToSearch,&#xD;
	const char* pCharsToCount)&#xD;
{&#xD;
	unsigned int numCharsFound = 0;&#xD;
	std::string strToSearch = pStringToSearch;&#xD;
	size_t lastIndexFound = -1;&#xD;
	while (&#xD;
		(lastIndexFound = strToSearch.find_first_of(&#xD;
			pCharsToCount,&#xD;
			lastIndexFound+1)) != std::string::npos)&#xD;
	{&#xD;
		++numCharsFound;&#xD;
	}&#xD;
	return numCharsFound;&#xD;
} &#xD;
//---------------------------&#xD;
// End example.&#xD;
//---------------------------&#xD;
&#xD;
(Again, it kills my indenting,  sorry for that... I'm considering using something like underscores , ala "_____" to indent out. Or just complain to tribe.net... A non-proportinal font capable input box with whitespace should be easy enough... Sigh...)&#xD;
&#xD;
--Peace,&#xD;
----Specular.</description>
      <pubDate>Tue, 23 Aug 2005 07:35:59 GMT</pubDate>
      <guid isPermaLink="false">http://CPP.tribe.net/thread/11e02fd7-3a81-468a-b637-3620c29c12d5#9399892f-0af4-44d9-aaab-4827cd283f51</guid>
      <dc:creator>Specular</dc:creator>
      <dc:date>2005-08-23T07:35:59Z</dc:date>
    </item>
    <item>
      <title>Re: lost in strings</title>
      <link>http://CPP.tribe.net/thread/11e02fd7-3a81-468a-b637-3620c29c12d5#f3cc3711-f3d1-485a-9b27-3a762cb7dc69</link>
      <description>P.S. Bummer, it took my oh-so-careful indenting out of my code example... Oh well.. Add tab/space intents to my code, of course.&#xD;
&#xD;
--Specular.</description>
      <pubDate>Tue, 23 Aug 2005 07:05:01 GMT</pubDate>
      <guid isPermaLink="false">http://CPP.tribe.net/thread/11e02fd7-3a81-468a-b637-3620c29c12d5#f3cc3711-f3d1-485a-9b27-3a762cb7dc69</guid>
      <dc:creator>Specular</dc:creator>
      <dc:date>2005-08-23T07:05:01Z</dc:date>
    </item>
    <item>
      <title>Re: lost in strings</title>
      <link>http://CPP.tribe.net/thread/11e02fd7-3a81-468a-b637-3620c29c12d5#2e8563ba-647b-41b3-9f23-885e63bd1dce</link>
      <description>the stl style string class can help here...&#xD;
&#xD;
So... You could use std::string, and it's find_first_of member function. Like this.&#xD;
&#xD;
I haven't compiled this. There are likely bugs... But it gets the idea across. For how to search a string for chars, count them, etc...&#xD;
&#xD;
If you have visual studio.net, I'd reccomend using the MSDN help files and reading all about std::string and std::basic_string (it's base class) member functions. They can take the hurt out of applications like this. They also can be gotten to play nicely with the &amp;amp;lt;iostream&gt; classes/operators, such that you can do stuff like parse a dictionary into a list of "words" and "sort them" with a few lines of code. I remember writing a full "anagram game" with IO, dictionaries (used "hounds of the baskervilles" for source words to scramble... Etc... all in about 200 lines of code with this sorta stuff.&#xD;
&#xD;
Anyhow, here's an example of how you might "count letters in one string that match any possible character in another string"&#xD;
&#xD;
//------------------------------------------------------------------------------&#xD;
// Begin example.&#xD;
//------------------------------------------------------------------------------&#xD;
unsigned int CountChars(&#xD;
  const char* pStringToSearch,&#xD;
  const char* pCharsToCount)&#xD;
{&#xD;
  // Bail if we have a bad case. Using 0xFFFFFFFF as an "error code."&#xD;
  if (!pStringToSearch || !pCharsToCount) return 0xFFFFFFFF;&#xD;
&#xD;
  // Search for chars.&#xD;
  unsigned int numCharsFound = 0;&#xD;
  unsigned int lastIndexFound = 0;&#xD;
  bool bContinueSearch = true;&#xD;
  std::string strToSearch = pStringToSearch;&#xD;
&#xD;
  while (bContinueSearch)&#xD;
  {&#xD;
    // This function call searches strToSearch for the first&#xD;
    //  matching character in pCharsToCount. It is using&#xD;
    //  lastIndexFound as a starting index for its search,&#xD;
    //  and updating that same variable through its return&#xD;
    //  to reflect a new find, or to terminate the search in&#xD;
    //  the case of "npos"&#xD;
    lastIndexFound = strToSearch.find_first_of(&#xD;
      pCharsToCount ,&#xD;
      lastIndexFound);&#xD;
    if (lastIndexFound == std::string::npos)&#xD;
    {&#xD;
      // Break search, end of string.&#xD;
      bContinueSearch = false;&#xD;
    }&#xD;
    else&#xD;
    {&#xD;
      // We found a matching character.&#xD;
      ++numCharsFound;&#xD;
    }&#xD;
  }&#xD;
&#xD;
  // Return how many matching characters we found.&#xD;
  return numCharsFound;&#xD;
}&#xD;
&#xD;
//------------------------------------------------------------------------------&#xD;
// End example.&#xD;
//------------------------------------------------------------------------------&#xD;
&#xD;
So, inputing&#xD;
("Search this string for s's S's, commas, P's and p's.", "Ss,Pp"); &#xD;
should return 13.  (if I counted right, heh...)&#xD;
&#xD;
Whereas, the same input, but different search key: (realy only looking for commas this time)&#xD;
("Search this string for s's S's, commas, P's and p's.", ","); &#xD;
should return 2...&#xD;
&#xD;
With this function, and some searching of your whole input file/page/whatever, and some simple math, you should be able to find words.. (Like search for " " as a word-break maybe.) And sentances. (Could search for ".!?" As sentance delimiters...&#xD;
&#xD;
--Hope this helps.&#xD;
----Specular.</description>
      <pubDate>Tue, 23 Aug 2005 07:03:06 GMT</pubDate>
      <guid isPermaLink="false">http://CPP.tribe.net/thread/11e02fd7-3a81-468a-b637-3620c29c12d5#2e8563ba-647b-41b3-9f23-885e63bd1dce</guid>
      <dc:creator>Specular</dc:creator>
      <dc:date>2005-08-23T07:03:06Z</dc:date>
    </item>
    <item>
      <title>Re: lost in strings</title>
      <link>http://CPP.tribe.net/thread/11e02fd7-3a81-468a-b637-3620c29c12d5#50322926-393c-4238-9491-4d0346ed11ff</link>
      <description>and look at functions like toupper and tolower which should help doing case insentive comparisons on single chars or stricmp() for strings.</description>
      <pubDate>Mon, 27 Jun 2005 22:13:04 GMT</pubDate>
      <guid isPermaLink="false">http://CPP.tribe.net/thread/11e02fd7-3a81-468a-b637-3620c29c12d5#50322926-393c-4238-9491-4d0346ed11ff</guid>
      <dc:creator>Hypno</dc:creator>
      <dc:date>2005-06-27T22:13:04Z</dc:date>
    </item>
    <item>
      <title>Re: lost in strings</title>
      <link>http://CPP.tribe.net/thread/11e02fd7-3a81-468a-b637-3620c29c12d5#a00db3c0-e660-4da0-8455-2cdbaa504d65</link>
      <description>Also, since it appears you have VS .net. Try getting help on the C++ Library functions, isalpha(), isalphanum() isspace().  Once you find help on one of these functions at the bottom will be a link to get help on all is/isw functions.&#xD;
&#xD;
A simple method would be to scan your string char by char up to strlen() and write a case statement handling special characters and incereasing your counts.</description>
      <pubDate>Mon, 27 Jun 2005 22:10:21 GMT</pubDate>
      <guid isPermaLink="false">http://CPP.tribe.net/thread/11e02fd7-3a81-468a-b637-3620c29c12d5#a00db3c0-e660-4da0-8455-2cdbaa504d65</guid>
      <dc:creator>Hypno</dc:creator>
      <dc:date>2005-06-27T22:10:21Z</dc:date>
    </item>
    <item>
      <title>Re: lost in strings</title>
      <link>http://CPP.tribe.net/thread/11e02fd7-3a81-468a-b637-3620c29c12d5#88aaad27-134b-4137-94c1-1e7601c15abe</link>
      <description>Hi Phantom, you should take a look at how C functions like atoi and itoa work. Each character in a string array has an ASCII value, so take a look at the ASCII table. Notice that uppercase alphabet values range from 64 to 90, while lowercase ones are between 97 and 122.</description>
      <pubDate>Sat, 25 Jun 2005 20:35:30 GMT</pubDate>
      <guid isPermaLink="false">http://CPP.tribe.net/thread/11e02fd7-3a81-468a-b637-3620c29c12d5#88aaad27-134b-4137-94c1-1e7601c15abe</guid>
      <dc:creator>$item.owner.firstName</dc:creator>
      <dc:date>2005-06-25T20:35:30Z</dc:date>
    </item>
    <item>
      <title>lost in strings</title>
      <link>http://CPP.tribe.net/thread/11e02fd7-3a81-468a-b637-3620c29c12d5#1636d9e6-560f-4d35-9664-2ac9f4181a27</link>
      <description>I have a program that I know I need a string to use. Problem is in class we did a program with strings in which we input one name like "Dan" and another name like "dave" and it would say which is bigger, how many characters are in each ect. This new program I have to ask the user to input a sentence like.. " This book has ten pages" then it would count the number words and senteces in text. Also I would have to ask the user for a letter like "a" and it would count the lower and upper case letters. I have a program in VB where i would put "Hello" and it tels me how many times "L" is used. (i cant find that disk  )&#xD;
&#xD;
I have seen programs in which they count words from text from a file. The program needs to work like microsoft word (where when you go to word count it tells you all those things).</description>
      <pubDate>Sat, 25 Jun 2005 17:54:49 GMT</pubDate>
      <guid isPermaLink="false">http://CPP.tribe.net/thread/11e02fd7-3a81-468a-b637-3620c29c12d5#1636d9e6-560f-4d35-9664-2ac9f4181a27</guid>
      <dc:creator>Phantom</dc:creator>
      <dc:date>2005-06-25T17:54:49Z</dc:date>
    </item>
  </channel>
</rss>



