Home > Back-end >  How to loop through certain Ascii characters in c
How to loop through certain Ascii characters in c

Time:09-22

I only want to loop through certain Ascii characters and not all are directly next to each other . For example I only want to loop from char '1 to 7' and then from char '? to F'. I dont want to loop through '8 to >' . I have this for loop but this will include the char I don't want.

for (char i = '1'; i < 'H'; i  )

How should I modify it to only loop through what I want?

CodePudding user response:

Looping from 1 to 7 is straight-forward, since the arabic numerals ('0' to '9') are required to continguous and increasing values by all C and C standards.

for (char c = '1'; c <= '7';   c)

or (a more common style)

for (char c = '1'; c < '8';   c)

The problem with trying to loop through your second set of ASCII characters ('?' to 'F') is that there are character sets other than ASCII - in which the order of characters is different. For example, in ASCII, '?' is one less than '@', but that is not guaranteed for other character sets. Instead, create a string with the characters you want to loop over, and iterate over the string. For example;

const std::string characterset = "?@ABCDEF";

for (char c : characterset)      // option 1, C  11 and later
{
     // do something with c
}

for (auto c : characterset)      // option 2, C  11 and later (type deduction)
{
     // do something with c
}

// Option 3 (all C   standards)

for (std::string::const_iterator it = characterset.begin(), end = characterset.end();
     it != end;   it)
{
    char c = *it;
    // do something with *it or c (it is an iterator that references a character)
}

will loop over your second set of characters.

If you want to do it all in a single loop, then change the character set. For example, a modified version of Option 1 above might be;

const std::string characterset = "1234567?@ABCDEF";

This is a more general approach that doesn't rely on your implementation (host system, compiler, library) supporting the ASCII character set (or compatible).

CodePudding user response:

Create a set containing the characters you want to loop over and loop over that set. For example :

#include <iostream>
#include <stdexcept>
#include <string>
#include <set>

// character_set.h
//-------------------------------------------------------------------------
// To be able to easily input a character range we need a helper struct

struct character_range_t
{
    // have this destructor so a character range can be used in brace initialization.
    character_range_t(const char f, const char t) :
        from(f),
        to(t)
    {
        if (to < from) throw std::invalid_argument("to must be larger or equal to from");
    }

    char from;
    char to;
};

//-------------------------------------------------------------------------
// helper function to combine multiple character ranges into on set
// input is a compile time array of ranges

template<std::size_t N>
auto make_character_set(const character_range_t(&ranges)[N])
{
    // I chose a set because all elements must be unique and set does that.
    std::set<char> set;

    // loop over all input ranges
    for (std::size_t n = 0; n < N;   n)
    {
        // and for each range add the characters in the range to the set
        for (char c = ranges[n].from; c <= ranges[n].to;   c) set.insert(c);
    }

    return set;
}

// main.cpp 
//-------------------------------------------------------------------------
// #include "character_set.h"

int main()
{
    auto set = make_character_set({{'1','7'},{'?','F'}});

    // use range based for loop to loop over all characters in the set
    for (const char c : set)
    {
        std::cout << c << " ";
    }
}

CodePudding user response:

Each character has a fixed ASCII value associated with it. You can refer to any character with that particular ASCII value. You can just skip the characters you do not want with an 'if' condition. You will find all the ASCII values here. Referring to your example, if you want to skip the characters from '?' to 'F', the code might look something like this:

#include <iostream>

using namespace std;

int main()
{
    for (char i = '1'; i < 'H'; i  )
    {
        if(i>=63 && i<=70)
        // 63 is the ASCII value for '?'
        // 70 is the ASCII value for 'F'
        {
            // skipping the ASCII values we do not need
            continue;
        }
    
        cout << i << "";
    }

    return 0;
}
  • Related