Segmentation fault in rot13 code (C)

4
#include <stdio.h>
#include <string.h>
char rot13(char palabra[]) { //char *palabra
    int y = (short) strlen(palabra);
    char abc[27]="abcdefghijklmnopqrstuvwxyz";
    for (int i = 0; i < y ; ++i) {
        if(palabra[i]<'m' && palabra[i]>='a'){
            for (int j = 0; j <26 ; ++j) {
                if (palabra[i]==abc[j]){
                    palabra[i]=abc[j+13];
                }

            }

        }

    }
    return palabra;
}

int main() {
    rot13("aaa");
    return 0;
}

Hi, I'm doing a code in c that makes ROT13 encryption, for that my idea is to define the alphabet, go through it and exchange the corresponding characters adding 13 ... The code so far covers only the case that adds 13 and me pulls "Segmentation fault" error and also a warning that says:

main.c:18:12: warning: return makes integer from pointer without a cast [-Wint-conversion]
     return palabra;

And I do not understand what the problem is, in case someone can explain to me how to solve the problem and the warning, thank you in advance!

    
asked by noordic 05.10.2018 в 23:23
source

1 answer

1

There are two problems in your code. First, you call a function that returns something char and you are calling it as if it were void and ... if you want it to return a string of characters, it is not char, but char *, since what it does is create a variable that stores the string but, when returning, returns the pointer to the first character of the string.

I modified your code, I hope this serves you:

#include <stdio.h>
#include <string.h>

void rot13(char*,char*);

int main() {
    char prueba[50], enc[50];
    printf(" Ingrese una palabra: ");
    fflush(stdin);
    scanf("%s",prueba);
    rot13(prueba,enc);
    int i;
    for(i=0;i<strlen(enc);i++){
        printf("%c", enc[i]);
    }
    printf("\n");
    system("pause");
    return 0;
}

void rot13(char *palabra, char *resultado) {
    char abc[]="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
    int larp=strlen(palabra);
    int i=0, j=0;
    for(i=0;i<larp;i++){
        for(j=0;j<26;j++){
            if(palabra[i]==abc[j]){
                resultado[i]=abc[j+13];
            }
        }
    }
    resultado[i]='
#include <stdio.h>
#include <string.h>

void rot13(char*,char*);

int main() {
    char prueba[50], enc[50];
    printf(" Ingrese una palabra: ");
    fflush(stdin);
    scanf("%s",prueba);
    rot13(prueba,enc);
    int i;
    for(i=0;i<strlen(enc);i++){
        printf("%c", enc[i]);
    }
    printf("\n");
    system("pause");
    return 0;
}

void rot13(char *palabra, char *resultado) {
    char abc[]="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
    int larp=strlen(palabra);
    int i=0, j=0;
    for(i=0;i<larp;i++){
        for(j=0;j<26;j++){
            if(palabra[i]==abc[j]){
                resultado[i]=abc[j+13];
            }
        }
    }
    resultado[i]='%pre%';
}
'; }

What I do is in effect, declare rot13 as void but pass another vector of the same length as the vector where the word is, to save the encrypted text.

    
answered by 09.10.2018 в 18:50