Initialize a function that has a string pointer as argument

Asked

Viewed 359 times

0

I wrote a function that changes the letter of a string by its successor using a pointer of string.

#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 on the function startup shift_string() in the main(), because you are giving "Invalid Initializer" error in Dev C++.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

2 answers

1

Why are you declaring a string, so your initializer is double quote text, nothing else. The use of the keys makes you start what would be a array, which is not what you want, so it is invalid.

In C it is even possible to initialize a string as if it were a array of characters, because this is exactly what string, but then I couldn’t use the quotes, I would have to create face one of the characters as separate elements, would have to be each of them with simple quotes, and have a terminator manually placed.

You have to choose one or the other initializer (you can even use both in the appropriate scenario where you have one 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";

I put in the Github for future reference.

The code can be simplified and written in a more idiomatic way. It also has a conceptual and logic error. He’s changing the original and he’s not returning anything, but he seems to expect it to return.

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] != '\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;
}
  • Without a specific question regarding pointers, open a new question focusing on this particular question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.