Initialize a function that has a string pointer as an argument

0

I wrote a function that exchanges the letter of a string for its successor using a string pointer.

#include <stdio.h>
#include <stdlib.h>

/*
14) Implemente um algoritmo que receba uma string como parâmetro e substitua todas as letras
por suas sucessoras no alfabeto. Por exemplo, a string “Casa” seria alterada para “Dbtb”.
A letra z deve ser substituída pela letra a (e Z por A). Caracteres que não forem letras devem
permanecer inalterados.
*/

    void shift_string (char* str)
    {
        int i = 0;

        while (str[i] != 0)
        {
            if (str[i] == 'z') str[i] = 'a';
            else if (str[i] == 'Z') str[i] ='A';
            else if (str[i] >= 'a' && str[i] < 'z') str[i]++;
            else if (str[i] >= 'A' && str[i] < 'Z') str[i]++;
            i++;
        }
    }

    int main() {

        char original[10] = {"Casa"};
        char shifted[10] = shift_string(original);

        printf("%s\n", original);
        printf("%s\n", shifted);

        return 0;
    }

My question is the initialization of the function shift_string() in main() , because it is giving "Invalid Initializer" error in Dev C ++.

    
asked by anonymous 21.11.2018 / 18:26

2 answers

1

Why you are declaring a string , so your initializer is double-quoted text, nothing more. Using the keys causes you to initialize what would be an array , which is not what you want, so it is invalid.

In C it's even possible to initialize a string as if it were an array , because that's exactly what is the string , but there could not use the quotation marks, would have to create face one of the characters as separate elements, it would have to be each with single quotation marks, and have a terminator manually placed.

You have to choose either initializer (you can even use both in the appropriate scenario where you have array of strings , which is not the case), and in this case it seems that the simplest is to initialize only with the quotation marks:

char original[10] = "Casa";

The code can be simplified and written more idiomatically. And it still has a conceptual and logical error. It is changing the original and is not returning something, but it looks like it is expected to return.

    
21.11.2018 / 18:37
0

I was able to improve the logic of the code and make it work.

#include <stdio.h>
#include <stdlib.h>

/*
14) Implemente um algoritmo que receba uma string como parâmetro e substitua todas as letras
por suas sucessoras no alfabeto. Por exemplo, a string “Casa” seria alterada para “Dbtb”.
A letra z deve ser substituída pela letra a (e Z por A). Caracteres que não forem letras devem
permanecer inalterados.
*/

void shift_string (char str[50])
{
    int i = 0;

    while (str[i] != '
#include <stdio.h>
#include <stdlib.h>

/*
14) Implemente um algoritmo que receba uma string como parâmetro e substitua todas as letras
por suas sucessoras no alfabeto. Por exemplo, a string “Casa” seria alterada para “Dbtb”.
A letra z deve ser substituída pela letra a (e Z por A). Caracteres que não forem letras devem
permanecer inalterados.
*/

void shift_string (char str[50])
{
    int i = 0;

    while (str[i] != '%pre%')
    {
        if (str[i] == 'z') str[i] = 'a';
        else if (str[i] == 'Z') str[i] ='A';
        else if (str[i] >= 'a' && str[i] < 'z') str[i]++;
        else if (str[i] >= 'A' && str[i] < 'Z') str[i]++;
        i++;
    }
    puts(str);
}

int main() {

    char str[50] = "Casa";

    shift_string(str);

    return 0;
}
') { if (str[i] == 'z') str[i] = 'a'; else if (str[i] == 'Z') str[i] ='A'; else if (str[i] >= 'a' && str[i] < 'z') str[i]++; else if (str[i] >= 'A' && str[i] < 'Z') str[i]++; i++; } puts(str); } int main() { char str[50] = "Casa"; shift_string(str); return 0; }
    
21.11.2018 / 19:08