Advertisement
BenTibnam

Pointer Math Example

Sep 19th, 2023
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(void)
  5. {
  6.         // these are the letters we are going to change to uppercase
  7.         char    vowels[] = {'a', 'e', 'i', 'o', 'u'};
  8.  
  9.         // this is the string we are going to perform the operation on
  10.         char    *str = "Hello World";
  11.  
  12.         // iterator for the inner for-loop
  13.         int     i;
  14.  
  15.         // while we haven't reached the null terminator
  16.         for (; *str != '\0'; str++)
  17.         {
  18.                 // storing the current character we're checking
  19.                 char    current_char = *str;
  20.  
  21.                 // looping through to see if the character is a lowercase vowel
  22.                 for (i = 0; i < 5; i++)
  23.                 {
  24.                         char    current_vowel = vowels[i];
  25.  
  26.                         // if it is, make it upper case
  27.                         if (current_char == current_vowel)
  28.                         {
  29.                                 // 32 is the delta between 'a' and 'A' or 97-65
  30.                                 current_char -= 32;
  31.                         }
  32.                 }
  33.  
  34.                 printf("%c", current_char);
  35.         }
  36.  
  37.         printf("\n");
  38.         return 0;
  39.  
  40. }
Tags: pointers
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement