954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how can i find out some char from string?

i just want to find out '?','/','\', ect, from a char array, can anyone help me to find out what's wrong with the code below?

thanks

the reture value is not always what i want?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

static int is_valid_function_name(char *function_name_string)
{

for( ; *function_name_string !=' \0 ' ; function_name_string++ )
{
if ( *(function_name_string) ==' ? ' )

/* (*(function_name_string) ==' / ')||
(*(function_name_string ==' \\ ' )||
(*(function_name_string) ==' : ' )||
(*(function_name_string) ==' * ' )||
(*(function_name_string) ==' < ' )||
(*(function_name_string) ==' > ')||
(*(function_name_string) ==' | ')||
(*(function_name_string) ==' " ') */

{
return 0;
break ;
}


}
return 1;
}

see_moonlight
Newbie Poster
7 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

I would try it this way:

#include <iostream>

static int is_valid_function_name(const char *function_name_string){
   for( ; *function_name_string !='\0' ; function_name_string++ ){
      switch (function_name_string[0]) {
         case '?' :
         case '/' :
         case '\\' :
	 case ':' :
         case '*' :
         case '<' :
         case '>' :
         case '\"' :
           return 0;
         default:
           break;
      }
   }
   return 1;
}
int main() {
   const char * legal_string = "there are no special characters";
   const char * non_legal_string = "there are special < characters ?";
   std::cout << "\"" << legal_string;
   if ( is_valid_function_name( legal_string ) )
       std::cout << "\" is a valid function name" << std::endl;
   else
       std::cout << "\" is not a valid function name" << std::endl;

   std::cout << "\"" << non_legal_string;
   if ( is_valid_function_name( non_legal_string ) )
       std::cout << "\" is a valid function name" << std::endl;
   else
       std::cout << "\" is not a valid function name" << std::endl;
   return 0;
}

K.

ZuK
Light Poster
29 posts since Oct 2004
Reputation Points: 11
Solved Threads: 0
 

I much prefer this one:

#include <cstring>

static int is_valid_function_name(const char *function_name_string){
   return std::strpbrk(function_name_string, "?/\\:*<>\"") == NULL;
}

;)

By the way, qualifiying a global name as static to give it internal linkage is a deprecated feature. If this was the original intent of the function--as opposed to a static member function declared as inline--then an unnamed namespace is the recommended option:

namespace {
   int is_valid_function_name(const char *function_name_string){
      return std::strpbrk(function_name_string, "?/\\:*<>\"") == NULL;
   }
}

Though, ZuK, it strikes me that the original code may have been C instead of C++. In that case you may have just confused the OP more. If it really was C++ then I wonder why the return value was not bool.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

i want to know if a function name is standard c name or not, what can i do from this code? i.e. a function name can be A~z ,0~9 and _, but can not begin wih 0~9 ?

see_moonlight
Newbie Poster
7 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You