| | |
how can i find out some char from string?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 7
Reputation:
Solved Threads: 0
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;
}
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;
}
•
•
Join Date: Oct 2004
Posts: 29
Reputation:
Solved Threads: 0
I would try it this way:
K.
C++ Syntax (Toggle Plain Text)
#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; }
I much prefer this one:

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:
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.
C++ Syntax (Toggle Plain Text)
#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:
C++ Syntax (Toggle Plain Text)
namespace { int is_valid_function_name(const char *function_name_string){ return std::strpbrk(function_name_string, "?/\\:*<>\"") == NULL; } }
I'm here to prove you wrong.
![]() |
Similar Threads
- get next char/string from files (Java)
- Char/String problems (C++)
- Need help finding index of char in a string (Java)
- Find and replace string function (C++)
- Pls help :Find first non-repeated char in a string (C++)
- char = string??? (Java)
- how to find length of a string (C++)
Other Threads in the C++ Forum
- Previous Thread: Problem with while loop... probably common
- Next Thread: I need a project ;P
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






