dimanche 29 mars 2015

Function to encrypt a string using pointers

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");
}

Aucun commentaire:

Enregistrer un commentaire