how can i find out some char from string?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 7
Reputation: see_moonlight is an unknown quantity at this point 
Solved Threads: 0
see_moonlight see_moonlight is offline Offline
Newbie Poster

how can i find out some char from string?

 
0
  #1
Oct 30th, 2004
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;
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 29
Reputation: ZuK is an unknown quantity at this point 
Solved Threads: 0
ZuK ZuK is offline Offline
Light Poster

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

 
0
  #2
Oct 30th, 2004
I would try it this way:
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,783
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 745
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #3
Oct 30th, 2004
I much prefer this one:
  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:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 7
Reputation: see_moonlight is an unknown quantity at this point 
Solved Threads: 0
see_moonlight see_moonlight is offline Offline
Newbie Poster

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

 
0
  #4
Oct 31st, 2004
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 ?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC