1

The code is supposed to translate certain characters into other characters.

My issue is this, when inputting characters to translate, it does it fine but only up to the 8th character.

Example:

AAAAAAAAA is supposed to be @@@@@@@@@,

but what it translates to is

@@@@@@@@A.

Can anyone help?

#include void secrefy(char* secret) { int b = sizeof(secret); for(int a = 0; a < b; a++) { switch(secret[a]) { case 'a': secret[a] = '@'; break; case 'A': secret[a] = '@'; break; case 'e': secret[a] = '3'; break; case 'E': secret[a] = '3'; break; case 'h': secret[a] = '#'; break; case 'H': secret[a] = '#'; break; case 'p': secret[a] = 'q'; break; case 'P': secret[a] = 'q'; break; case 's': secret[a] = '$'; break; case 'S': secret[a] = '$'; break; case 't': secret[a] = '+'; break; case 'T': secret[a] = '+'; break; case 'x': secret[a] = '*'; break; case 'X': secret[a] = '*'; break; } } } int main(void) { char secret[]="AAAAAAAAA"; secrefy(secret); printf("Encrypted will be %s", secret); } 
2
  • 5
    sizeof(secret); is only size of pointer , which is not equal to your passed string inside your function secrefy(), better pass size while calling function or use strlen inside secrefy() Commented Dec 11, 2020 at 3:46
  • 2
    as a general rule in C, if you pass an array to a function, always have an extra parameter for the size of the array since the array decays to a pointer so sizeof will not give you the size of the array but instead gives you the size of the pointer. Commented Dec 11, 2020 at 3:59

1 Answer 1

4

Instead of b = sizeof(secret) you need to use b = strlen(secret).

sizeof operator gives the size of type (char * in this case).

strlen function returns the length of string by reading string contents.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.