5 Replies - 1835 Views - Last Post: 28 February 2010 - 11:56 PM Rate Topic: -----

#1 cyfuzhi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 27-February 10

Searching for an invalid character in cstring

Posted 28 February 2010 - 09:27 PM

Hi all,
I'm having some trouble getting this loop to work correctly when a user enters a character that's not part of the cstring. It's working if the char is part of the cstring.

int cStringLength(char* p_cString)
{
	int charCounter = 0;
	while ('\0' != p_cString[charCounter])
	{	charCounter++;
	}
	return charCounter;
}

int find(char* p_cString, char valueToFind)
{
	char* currentElementPtr = p_cString;
	currentElementPtr = strchr (p_cString, valueToFind);
	while (currentElementPtr != '\0')
	{
		cout << "\n\nSearch resulted in index of " << currentElementPtr - p_cString << ".\n";
		currentElementPtr = strchr (currentElementPtr + 1, valueToFind);
		cout << "(any number outside of 0 to " << cStringLength(p_cString);
		cout <<	" means that '" << valueToFind << "' does not exist in\n\"";
		cout << p_cString << "\")"".\n\n";
	}
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Searching for an invalid character in cstring

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Searching for an invalid character in cstring

Posted 28 February 2010 - 10:18 PM

Change this line:
char* currentElementPtr = p_cString;


To this:
char* currentElementPtr; // don't assign this to anything


It should work as it was taken from this example:
http://cplusplus.com...cstring/strchr/
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Searching for an invalid character in cstring

Posted 28 February 2010 - 10:47 PM

eker676:
That change doesn't make any difference. The assignment of
currentElementPtr = p_cString;
is useless because its value is reassigned on the next line anyway, so your suggested change has no effect on the program's output.


cyfuzhi:
The program, as it is, behaves correctly: when the char that is entered is not found in the string, the pointer returned by strchr is NULL so the while loop is not entered and nothing is written to cout. What did you want it to do?
Was This Post Helpful? 0
  • +
  • -

#4 cyfuzhi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 27-February 10

Re: Searching for an invalid character in cstring

Posted 28 February 2010 - 11:12 PM

Right, it didn't change a thing.

It's working correctly when the user is prompted to enter a cstring for example, "c plus plus". Then it'll prompt the user to enter a char to search its index. Find 'u'. It's sending out the correct index for a character from the cstring.

But it's not displaying the statements if the user enters a char to find, such as 'x', which is not part of the cstring "c plus plus".
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Searching for an invalid character in cstring

Posted 28 February 2010 - 11:36 PM

The code that you posted is not "supposed" to display anything when the user enters a char that is not part of the string, because in that case strchr sets currentElementPtr = 0 and the program doesn't enter the while loop.

If you want a message printed in that situation, you can put something like this BEFORE the while statement:
if (currentElementPtr == 0)
{
    cout << valueToFind << " does not exist in the string\n\"" << p_sString << "\".\n";
}

Was This Post Helpful? 1
  • +
  • -

#6 cyfuzhi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 27-February 10

Re: Searching for an invalid character in cstring

Posted 28 February 2010 - 11:56 PM

Thanks r.stiltskin!

It's working perfectly now!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1