-1

I'm trying to add an int to each char in a char array but I don't know how to do that.

void Offset(char sirDeCaractere[100], int x)
{
    int i = 0;
    while (sirDeCaractere[i] != '\0')
    {
        sirDeCaractere[i] += x;
        i++;
    }
    printf("%c", sirDeCaractere[i]);
}

I've been trying to do something like this for a while, but I can't figure it out. Written like this I don't think it will work due to pointer arithmetic, but I have no other ideas.

For example:

Input:
sirDeCaractere = cuvant
x = 5
Output:
hz{fsy

How should I edit my code in order to achieve the desired output?

Right now my program is crashing (as I said, because sirDeCaractere[i] += x; is pointer arithmetic).

Thanks.

  • 3
    Please show a complete [mre]. – mkrieger1 Feb 19 '21 at 07:56
  • Your logic is confusing: `'c' + 3` is `'f'`, but you want `'h'`? – David Ranieri Feb 19 '21 at 07:59
  • I don't understand how your input `cuvant` is related to the output `hz{fsy`. `sirDeCaractere[i] += x` is n ot pointer arithmetic. The code you show looks more or less correct, but `printf("%c", sirDeCaractere[i]);` makes no sense. – Jabberwocky Feb 19 '21 at 07:59
  • @DavidRanieri I'm sorry. I wrote x = 3 instead of x = 5. That was a typo –  Feb 19 '21 at 08:00
  • @OctavianNiculescu [edit] and correct. – Jabberwocky Feb 19 '21 at 08:01
  • @Jabberwocky I did. Sorry. –  Feb 19 '21 at 08:01
  • Switch from `printf("%c", sirDeCaractere[i]);` to `printf("%s\n", sirDeCaractere);` and you are done. – David Ranieri Feb 19 '21 at 08:02
  • @DavidRanieri It's crashing. And if I use the debugger I get "Access violation writing on sirDeCaractere[i] += x;" –  Feb 19 '21 at 08:03
  • Sorry but what is the goal of the function ?? Can you give an example before/after ?? – Alxbrla Feb 19 '21 at 08:04
  • 2
    Then the problem is in the code you are not showing, how do you flll and pass `sirDeCaractere`? – David Ranieri Feb 19 '21 at 08:04
  • @OctavianNiculescu Do you pass this function a pointer to a string literal? If so, that's your problem. Literals are constants and this function modifies the thing it is passed a pointer to. You can't modify a constant because it's ... constant. – David Schwartz Feb 19 '21 at 08:07
  • @DavidSchwartz yes. That's what I did. I understand now and you're completely right. If you want, you can add your answer, I will accept it because it was the most relevant. –  Feb 19 '21 at 08:09

1 Answers1

2

You want this:

#include <stdio.h>

void Offset(char sirDeCaractere[100], int x)
{
    int i = 0;
    while (sirDeCaractere[i] != '\0')
    {
        sirDeCaractere[i] += x;
        i++;
    }
    // remove this line, it makes no sense    printf("%c", sirDeCaractere[i]);
}

int main(void)
{
   char test[] = "ABCD";
   Offset(test, 1);
   printf("%s\n", test);
}

Output:

BCDE

I guess the code you didn't show is something like this:

...
char *test = "ABCD";
Offset(test, 1);
...

or

Offset("ABCD", 1);

which will crash on most platforms. Read this form more information: Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?

Bonus:

Be aware that the [100] below is useless:

void Offset(char sirDeCaractere[100], int x)

you can write:

void Offset(char sirDeCaractere[], int x)

or :

void Offset(char *sirDeCaractere, int x)

which do exactly the same thing.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115