0

I need to write a function that gets a string and a number N, the function will return in the same pointer the encrypted string. The function will encrypt a string by the following rules:

  1. Reverse the string, e.g: if the string is "Amnon" the result would be "nonmA".
  2. After the reverse, each letter needs to be replaced by the value of N, e.g: if N=3 then instead of "nonmA" the result would be "qrqpD".

I had no problem doing the reverse part but I'm struggling with switching each letter. Here is the code I wrote so far:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void StRreverse(char* mystr, int N);

void StRreverse(char* mystr, int N)
{
    int c, length, n;
    char *begin, *end, temp;
    length = strlen(mystr);
    begin = mystr;
    end = mystr;
    for (c = 0; c < length - 1; c++)
    {
        end++;
    }
    for (c = 0; c < length / 2; c++)
    {
        temp = *end;
        *end = *begin;
        *begin = temp;
        begin++;
        end--;
    }
}
void main()
{
    char string[100];

    printf("Enter some text\n");
    gets(string);

    StRreverse(string);
    printf("Reverse of entered string is \"%s\".\n", string);
    system("pause");
}
7
  • 2
    void main() should be int main(void) and never use gets as it is dangerous because it doesn't prevent buffer . Use fgets instead. Commented Mar 29, 2015 at 9:55
  • You need to pass two arguments to StReverse(), but you give it just one argument. Commented Mar 29, 2015 at 9:56
  • where's the 2nd part ? Commented Mar 29, 2015 at 9:56
  • 2
    What would the encrypted string be if the string to be encrypted is "XYZ" and N=3? Commented Mar 29, 2015 at 10:00
  • 1
    @AmnonHanuhov , shouldn't it be CBA ( after reversing ) ? Commented Mar 29, 2015 at 10:14

2 Answers 2

2

First, identify 'letters' in a portable way:

#include <ctype.h>

Then you can use isalpha(mystr[c]) to generally identify letters, and specifically islower and isupper. Then, on each letter, add your constant modulo 26. That is, if a becomes b, then y becomes z and z would be a again.

This operation is encoding-dependent as not all encodings have "a" to "z" and "A" to "Z" defined consecutively. Fortunately, it's unlikely you have such a system :) (although it is an interesting exercise to work out a way where this would not be an issue!).

The trick is to 'wrap around' with an offset of either A or a, so capitals and lowercase need a separate line of code:

if (isupper(mystr[c]))
   mystr[c] = 'A' + ((mystr[c]-'A' + N + 26) % 26);
if (islower(mystr[c]))
   mystr[c] = 'a' + ((mystr[c]-'a' + N + 26) % 26);

where N can be as low as -25, or as high as you want. That lower limit (and the addition of 26 in the statements) is because in some implementations of C, taking the modulus of a negative number returns a negative number as well.

To decode this text -- properly called the Caesar Cypher --, you can apply the same formula with -N.

Sign up to request clarification or add additional context in comments.

2 Comments

I have not yet studied isupper and islower so I assume I am not allowed to do that, is there any other method?
I think without using these functions the code should look like this: for (c = 0; c<length; c++) { if ((mystr[c] >= 'A') && (mystr[c] <= 'Z')) { mystr[c] = 'A' + ((mystr[c] - 'A' + N + 26) % 26); } if ((mystr[c] >= 'a') && (mystr[c] <= 'z')) { mystr[c] = 'a' + ((mystr[c] - 'a' + N + 26) % 26); } }
0

The most simple way to do switching is by adding the number to that character. For example

`char str[n] = {'a','b','c','d'};

for(int i=0; i less than n; i++) str[i] = str[i] +3; // we are adding 3 to switch `

I dont remeber the ascii number for small 'a'. But if its 36 the above code will make it 39 which will be 'd'. Hope it works for u

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.