Home > Software design >  How would I be able to run code for each character in a const char*?
How would I be able to run code for each character in a const char*?

Time:03-20

I am programming a kernel in C for a custom operating system that I'm making and I was wondering how I could loop for every character in a const char* and then use if operators to identify a character in the const char* to run specific code.

CodePudding user response:

how I could loop for every character in a const char*

Like that:

const char *p;
for (p = str; *p; p  ) {
    // ...
}

and then use if operators to identify a character in the const char* to run specific code.

Inside your loop:

if (*p == 'x') { // Identify a character
    // Run the specific code here
}

Or, if you want to identify a lot of characters:

switch (*p) {
case 'x':
    // Run the specific code here
    break;

case 'y':
    // Run the specific code here
    break;

// ...
}
  • Related