[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

comparing an array of strings to an input string

Posted on 2004-10-30
5
Medium Priority
?
249 Views
Last Modified: 2010-04-01
I have an array of strings like, letters[] = {"a","b"...
i have an input string that I want to do a sanity check on, say "microsoft"
I have a non working subroutine called
void setcompanyName (string mycompanyName) {

for (i=0; i<count;i++){
for( j=0; j<52; j++){
if (data[i] == letters[j]){
companyname = mycompanyName;            
}else {
cout<<"you have entered an incorrect name, setting to: \n";
}
}
}      
}
the code should loop through the word microsoft, should loop through the letter array and do a comparison to make sure no numeric or symbols were entered.  Good advice is grealy appreciated
0
Comment
Question by:police45s
5 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12454459
>> count, letters, mycompanyname

What's the definition of these variables? I'am assuming the following:

  int count = strlen(mycompynname);

  char* letters[52] = { "a", .... , "z", "A", ....., "Z" }

  char mycompanyname[32];

Yes? Look at the corrected function:

bool MyClass::setcompanyName (string mycompanyName)
{
    int   count     = strlen(mycompanyName);
    // define letters as char and not as char* (strings)
    char  letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (i=0; i<count;i++)
    {
        for( j=0; j<52; j++)
        {
            if (data[i] == letters[j])
                break;  // we break the j loop if we found a valid character
        }  
            if (j >= 52)
            {
                cout<<"you have entered an incorrect name, setting to: \n"
                return false;
            }
    }
    m_companyName = mycompanyName; // assuming class member
    return true;
}

Regards, Alex
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12454465
Correction: while posting i saw that mycompanyName is a string

Then, the solution turns to

bool MyClass::setcompanyName (string mycompanyName)
{
    int   count     = mycompanyName.length();
    // define all letters as string and not as char*
    string  letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (i=0; i<count;i++)
    {
        for( j=0; j<52; j++)
        {
            if (data[i] == letters[j])
                break;  // we break the j loop if we found a valid character
        }  
        if (j >= 52)
        {
            cout<<"you have entered an incorrect name, setting to: \n"
            return false;
        }
    }
    m_companyName = mycompanyName; // assuming class member
    return true;
}

Regards, Alex
0
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 375 total points
ID: 12455410
Seems like a job for isalpha().

0
 
LVL 17

Expert Comment

by:rstaveley
ID: 12456031
Brett's right about isalpha being a likely candidate for the job... but if you want to run a test with ranges of characters described by an arbitrary string, you can use basic_string's find_first_not_of.

Here's an absurdly over-engineered illustration using a "valid characters" string generated using the generate_n algorithm. Depending on your application, you might need to consider the validity of numeric characters and symbols like '&'.

--------8<--------
#include <iostream>
#include <string>
#include <functional>
#include <algorithm>
#include <iterator>

template<typename T> class PostIncrementGen {
      T t;
public:
      PostIncrementGen(T t) : t(t) {}
      T operator()() {
            return t++;
      }
};

namespace {

      // Valid characters - deployed as a function to allow static initialisation on first call
      const std::string& valid_characters()
      {
            // Valid character set
            static std::string match;

            // Needs to be generated
            if (match.empty()) {

                  // A-Z
                  generate_n(std::insert_iterator<std::string>(match,match.end()),'Z'-'A'+1,PostIncrementGen<char>('A'));

                  // a-z
                  generate_n(std::insert_iterator<std::string>(match,match.end()),'z'-'a'+1,PostIncrementGen<char>('a'));

                  // Space character also allowed
                  match += " ";
            }
            return match;
      }

}

// See if the spacified string is valid
bool valid(const std::string& str)
{
      return str.find_first_not_of(valid_characters()) == std::string::npos;
}

int main()
{
      {
            std::string str = "McAlpine Construction";
            std::cout << str << " is " << (valid(str)?"valid":"not valid") << '\n';
      }
      {
            std::string str = "ABC123 Taxi Cabs";
            std::cout << str << " is " << (valid(str)?"valid":"not valid") << '\n';
      }
      {
            std::string str = "EZ Cleaning";
            std::cout << str << " is " << (valid(str)?"valid":"not valid") << '\n';
      }
      {
            std::string str = "Jones & Co";
            std::cout << str << " is " << (valid(str)?"valid":"not valid") << '\n';
      }

}
--------8<--------
0
 
LVL 22

Expert Comment

by:grg99
ID: 12456709


I'd put the allowed characters into an old char[] and do a strchr():

char GoodChars[]= "abcdef...xyz_";   int i, HasBad;

for( i = 0, HasBad = 0; i < strlen( InputLine ); i++ ) HasBad = HasBad || strchr(  GoodChars, InputLine[i] ) == 0 );

 
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question