Home c++ Symbol Search in C++

Symbol Search in C++

Author

Date

Category

I won’t deal with the characters. There is a task – an array (generally a file, but it does not matter) with words. It is necessary to choose all words containing characters that are not letters or numbers (! @ # $% ^ & Amp; * and so on). Long climbed on the Internet, eventually wrote such a code:

# include "stdafx.h"
#Include & lt; fstream & gt;
#Include & lt; iostream & gt;
Using Namespace STD;
  INT MAIN ()
  {
    Char S [] = "D152313", * PS = S; // Array with words
    BOOL A = FALSE; // Flag talking about found symbol
    Char Au;
    for (int i = 0; i & lt; 256; i ++) {// cycle displays in console
      AU = I; COUT & LT; & LT; i & lt; & lt; "\ t" & lt; & lt; AU & LT; & LT; Endl; // Symbols and their codes
    }
// 48-57 numbers
// 65-90 A-Z
// 97-122 A-Z
// 128-175 A-P
// 224-241 district
    While (PS = STRCHR (PS, 51)) {// Check the array for the presence of figures 3
      if (PS! = NULL) {a = true; }
      STD :: COUT & LT; & LT; PS ++ - S + 1 & LT; & LT; "\ n"; // Tell the Troika (triples) in the array
    }
    if (a == true) {STD :: COUT & LT; & LT; "PROFIT"; }
    SYSTEM ("PAUSE");
    Return 0;
  }

In this case, it checks the array for the presence of a triple and says that it is found. The question is – how to check the range (and specifically it would be necessary to check the inconsistency of 5 bands, if 48-57, 65-90 and so on, then do nothing, otherwise the flag on True)? And in general, what do I do not really in the edge already so sad? I’m just learning while hard. I really hope that everything described everything is fine, and will help me before you delete the topic)


Answer 1, Authority 100%

To classify characters, you should use the STD :: ISALPHA , STD :: ISDIGIT , or from & lt; Locale & gt; or from & lt; CCType & gt; .

If you consider that words are a sequence of symbols separated by space symbols (\ t \ r \ n ), it is easiest to use STD :: ISTREAM . However, punctuation will also be considered part of the word, such as "ABC," . This can be fixed if you add punctuation to the list of space characters, How it is done here for ‘|’ .

Example with functions from & lt; Locale & GT; :

# include & lt; locale & gt;
#Include & lt; sstream & gt;
#Include & lt; algorithm & gt;
#Include & lt; iostream & gt;
STD :: Locale Locale (". 866");
BOOL IS_WORD (CHAR C) {
 RETURN STD :: ISALPHA (C, LOCALE) || STD :: ISDIGIT (C, LOCALE);
}
INT MAIN () {
              // "This is the text;" CP866.
 STD :: STRINGSTREAM STREAM ("\ XED \ XE2 \ XAE \ XE2 \ XA5 \ XAA \ Xe1 \ Xe2; AB1 C @ D QW? XYZ RS.");
 STD :: STRING WORD;
 While (Stream & GT; & GT; Word) {
  If (! Std :: All_of (Begin (Word), End (Word), Is_Word)) {
   STD :: COUT & LT; & LT; '[' & lt; & lt; Word & lt; & lt; "] \ n";
  }
 }
}

Similar code with & lt; CCType & gt;

# include & lt; clocale & gt; // Do not confuse C & LT; Locale & GT;
#Include & lt; CCType & gt;
BOOL IS_WORD (CHAR C) {
 Auto UC = Static_Cast & LT; unsigned char & gt; (c); // see below
 RETURN STD :: ISALPHA (UC) || STD :: ISDIGIT (UC);
}
INT MAIN () {
 STD :: SETLOCALE (LC_ALL, ".866");
              // "This is the text;" CP866.
 STD :: STRINGSTREAM STREAM ("\ XED \ XE2 \ XAE \ XE2 \ XA5 \ XAA \ Xe1 \ Xe2; AB1 C @ D QW? XYZ RS.");
 ... Next, as in the code above

It is necessary to note that the characters must be converted to unsigned char , because otherwise the std :: isdigit (int) will fall a negative value, and this leads to an uncertain behavior.


& lt; ctypes & gt; only single-byte encodings operate, and do not work for multibytes, such as UTF-8. The same is true for the & LT; LCALE & GT; – although there is support for wchar_t , but these functions take only one character, which means that UTF-16 does not fully support. Also, standard locals do not have full unicode support, for this, for example, not all characters are classified as numbers (一二三 123 ).


Answer 2

When checking compliance, the symbol should not go to a comparison of their codes, because In different encodings, different characters may have different numeric codes.
To check the symbol to belong to the letters there is a standard function Std :: Isalpha to numbers – Std :: ISDIGIT . On them and should be relying when deciding.


Answer 3

If you really need an answer to C++, then the solution of your task looks like about so :

# include & lt; iostream & gt;
#Include & lt; sstream & gt;
#Include & lt; vector & gt;
INT MAIN ()
{
  STD :: String Str = "The Quick Br1own F @ Ox Jumps Over T-He Lazy Dog.";
  Std :: IStringStream ISS (STR, STD :: IStringStream :: In);
  Std :: Vector & lt; Std :: String & GT; Words;
  While (ISS & GT; & GT; STR) Words.Push_Back (STR);
  FOR (AUTO & AMP; X: WORDS)
  {
    For (Size_t i = 0; i & lt; x.size (); i ++)
    {
      If (! STD :: ISDIGIT (X [I]) & amp; & amp;! STD :: ISALPHA (X [I]))
      {
        STD :: COUT & LT; & LT; x & lt; & lt; std :: endl;
        Break;
      }
    }
  }
  Return 0;
}

This code displays only those words, where other characters are found besides numbers and letters. But the main problem, you will come across – these are Russian letters (and any other, different from Latin). Specified range for Russian letters is faithful only for a specific code page.


Answer 4

Read the code with comments:

# include & lt; iostream & gt;
#Include & lt; String & GT;
#Include & lt; vector & gt;
Using Namespace STD;
// Return the truth if there are special characters in the word
BOOL ISINWORDNOTALPHAANDDIGIT (STRING S) {
  FOR (AUTO IT = S.BEGIN (); it! = S.End (); IT ++)
    if (! isalnum (* IT))
      RETURN TRUE;
  RETURN FALSE;
}
INT MAIN () {
  STRING STR = "ABC 111 AB1 AB $ A1 & amp; 1ab A & AMP; B A1B", BUF;
  Vector & lt; String & GT; vstr; // here we will lean the words found
  STR.PUSH_BACK (''); // In order not to think about the last iteration
  // Fit the line symbol cycle
  FOR (AUTO IT = STR.BEGIN (); it! = Str.end (); IT ++) {
    if (* it! = '') {// if not a space add to buffer
      buf.push_back (* IT);
    } else {
      if (buf! = "") // If the buffer is not empty
        VSTR.PUSH_BACK (BUF); // Found a gap Add buffer to an array
      buf.clear (); // Clean the buffer for the next word
    }
  }
  // We run through the cycle according to the array
  For (auto it = vstr.begin (); it! = vstr.end (); it ++) {
    if (ISINWORDNOTALPHAANDIGIT (* IT)) {// if there are special characters in the word
      COUT & LT; & LT; * IT & lt; & lt; Endl; // display the word on the screen
    }
  }
  Return 0;
}

Conclusion:

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions