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;  
}

Recommended Answers

All 3 Replies

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.

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.

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 ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.