943,945 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4102
  • C++ RSS
Oct 30th, 2004
0

how can i find out some char from string?

Expand Post »
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;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
see_moonlight is offline Offline
7 posts
since Oct 2004
Oct 30th, 2004
0

Re: how can i find out some char from string?

I would try it this way:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. static int is_valid_function_name(const char *function_name_string){
  4. for( ; *function_name_string !='\0' ; function_name_string++ ){
  5. switch (function_name_string[0]) {
  6. case '?' :
  7. case '/' :
  8. case '\\' :
  9. case ':' :
  10. case '*' :
  11. case '<' :
  12. case '>' :
  13. case '\"' :
  14. return 0;
  15. default:
  16. break;
  17. }
  18. }
  19. return 1;
  20. }
  21. int main() {
  22. const char * legal_string = "there are no special characters";
  23. const char * non_legal_string = "there are special < characters ?";
  24. std::cout << "\"" << legal_string;
  25. if ( is_valid_function_name( legal_string ) )
  26. std::cout << "\" is a valid function name" << std::endl;
  27. else
  28. std::cout << "\" is not a valid function name" << std::endl;
  29.  
  30. std::cout << "\"" << non_legal_string;
  31. if ( is_valid_function_name( non_legal_string ) )
  32. std::cout << "\" is a valid function name" << std::endl;
  33. else
  34. std::cout << "\" is not a valid function name" << std::endl;
  35. return 0;
  36. }
K.
ZuK
Reputation Points: 11
Solved Threads: 0
Light Poster
ZuK is offline Offline
29 posts
since Oct 2004
Oct 30th, 2004
0

Re: how can i find out some char from string?

I much prefer this one:
C++ Syntax (Toggle Plain Text)
  1. #include <cstring>
  2.  
  3. static int is_valid_function_name(const char *function_name_string){
  4. return std::strpbrk(function_name_string, "?/\\:*<>\"") == NULL;
  5. }


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)
  1. namespace {
  2. int is_valid_function_name(const char *function_name_string){
  3. return std::strpbrk(function_name_string, "?/\\:*<>\"") == NULL;
  4. }
  5. }
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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 31st, 2004
0

Re: how can i find out some char from string?

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 ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
see_moonlight is offline Offline
7 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problem with while loop... probably common
Next Thread in C++ Forum Timeline: I need a project ;P





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC