ahmed149 / learning-programing-in-C

0 stars 0 forks source link

ALX: a function that encodes a string using rot13 (rot13) #38

Open ahmed149 opened 1 year ago

ahmed149 commented 1 year ago

include "main.h"

/**

It checks if the current character in the input string str matches any character in the alphabet array. If a match is found, it replaces the current character in str with the corresponding character from the rot13key array. This effectively encodes the character according to the ROT13 cipher./ for (indx2 = 0; indx2 < 52; indx2++) { /The indx1 index is incremented to process the next character in the input string, and the loop continues until all characters have been processed.*/ if (str[indx1] == alphabet[indx2]) { str[indx1] = rot13key[indx2]; break; } }

    indx1++;
}

return (str);

}

ahmed149 commented 1 year ago

include "main.h"

char rot13_recursive_char(char c) { char alphabet[52] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; char rot13key[52] = {'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'};

for (int i = 0; i < 52; i++) {
    if (c == alphabet[i]) {
        return rot13key[i];
    }
}

// If the character is not found in the alphabet, return it unchanged
return c;

}

char rot13_recursive(char str) { // Base case: If the current character is the null terminator, we're done. if (*str == '\0') { return str; }

// Encode the current character using the rot13_recursive_char function
*str = rot13_recursive_char(*str);

// Make a recursive call to process the next character in the string
return rot13_recursive(str + 1);

}

ahmed149 commented 1 year ago

In this recursive version:

The rot13_recursive_char function takes a character c as input and returns its ROT13-encoded counterpart using the same alphabet and rot13key arrays as in the original function.

The rot13_recursive function is the main recursive function. It takes a character pointer str as input and returns a pointer to the modified (encoded) string.

The base case checks if the current character *str is the null terminator '\0', indicating the end of the string. If this is the case, the function simply returns str, signaling the end of the recursion.

Inside the function, the current character in the string str is encoded using the rot13_recursive_char function, and the result is stored back in the string.

A recursive call is made by moving to the next character in the string (str + 1).

The recursion continues until it reaches the end of the string, and then it starts returning back, effectively modifying the characters in the input string along the way.

You can use the rot13_recursive function just like the original version to encode a string using ROT13 recursively.