2

since my prof won't let me use RegEx, I'm stuck with using loops to check each character on a string. Does anyone have a sample code/algorithm?

public void setAddress(string strAddress)
{
    do
    {
        foreach (char c in Name)
        {
            if ( /*check for characters*/ == false)
            {
                Address = strAddress;
            }
        }
        if ( /*check for characters*/ == true)
        {
            Console.Write("Invalid!");
        }
    } while ( /*check for characters*/ == true)
}
public int getAddress()
{
    return Address;
}

I need to only include letters and numbers. Characters such as !@#$%^& are not allowed. I'm not allowed to use RegEx because he hasn't taught that to us yet... well I couldn't attend class on the day he taught these loops and character checking, so now he won't tell me more. ANYWAY, if there's a more efficient way without using RegEx, that'd be helpful.

Tam
  • 21
  • 1
  • 3
  • 3
    What do you mean "special character"? – Ray Toal Jul 29 '13 at 05:54
  • Can you show what invalid characters you are checking for.. also I don't see the need to do a do while loop combined with a foreach loop.. you can do this with a foreaach or a for loop just as easy or use a linq statement – MethodMan Jul 29 '13 at 05:55
  • `since my prof won't let me use RegEx` - you should tell him, as a programmer, you use whatever solves the problem in the most efficient way. – Dennis Jul 29 '13 at 05:56
  • like !@#$%^&*() and the likes. ANything that aren't letters or numbers basically. – Tam Jul 29 '13 at 05:57
  • if you insist on a for loop look at this link.. also try to think outside the box a little bit.. http://stackoverflow.com/questions/2208688/quickest-way-to-enumerate-the-alphabet-in-c-sharp – MethodMan Jul 29 '13 at 06:04
  • Difficult to think outside the box when you have a fever and still boligated to go to class :) but thanks! – Tam Jul 29 '13 at 06:10
  • Is there no textbook you could reference? – Gabe Jul 29 '13 at 06:30

4 Answers4

5
        string s = @"$KUH% I*$)OFNlkfn$";
        var withoutSpecial = new string(s.Where(c => Char.IsLetterOrDigit(c) 
                                            || Char.IsWhiteSpace(c)).ToArray());

        if (s != withoutSpecial)
        {
            Console.WriteLine("String contains special chars");
        }

You can do it without loops at all :)

Source: https://stackoverflow.com/a/4503614/1714342

EDIT:

if(s.Any(c=>c => !Char.IsLetterOrDigit(c) || !Char.IsWhiteSpace(c))
{
    Console.WriteLine("String contains special chars");
}
Community
  • 1
  • 1
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
  • also worth noting, that there will of course be loops - just hidden inside of the linq-part. Still, a very neat solution! – Dennis Jul 29 '13 at 05:58
  • 1
    @Chips_100 Of course there are loops below in linq, but you are not writing them by yourself. Loops are everywhere :D – Kamil Budziewski Jul 29 '13 at 05:59
  • 1
    Side note: [Any](http://msdn.microsoft.com/en-us/library/bb534972.aspx) is better for given task... Either approach unlikely to help OP so :) - but good answer for SO. – Alexei Levenkov Jul 29 '13 at 06:05
  • Ah, he'd likely question how I managed to come up with that solution without a loop. Although I might be able to use this in a future date. – Tam Jul 29 '13 at 06:09
2

You may not need loops at all, just character checking will do:

if (Name.IndexofAny("!@#$%^&*()".ToCharArray() != -1))
    Console.WriteLine("Valid Address");
else
    Console.WriteLine("Invalid Address");

See http://msdn.microsoft.com/en-us/library/system.string.indexofany.aspx

unlimit
  • 3,672
  • 2
  • 26
  • 34
0

A solution with explicit loop could be

  String s = @"$KUH% I*$)OFNlkfn$"; 

  foreach (Char c in s) 
    if (!(Char.IsLetterOrDigit(c) || Char.IsWhiteSpace(c))) {
      Console.WriteLine("String contains special chars");

      break;
    }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0
bool check_for_characters(char c)
{
 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}
0699
  • 250
  • 2
  • 11