0

I'm trying to re-index my ASCII decimal characters of the alphabet so that they start at 0 with 'A' rather than 65 so I can use a certain formula.

My initial thoughts were to create a string of the alphabet and iterate over it taking away minus 65 at each iteration, I then realised this is only having an effect on the string (which gives a segfault anyway) and does not have any effect on the decimal value of the actual character:

 string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";    
  
 for (int k = 0, length_ = strlen(alphabet); k < length_ ; k ++)
 {
      alphabet[k] = alphabet[k] - 65;
      printf("Alphabet no.%i is equal to %c", k, alphabet[k]);
 }

Any ideas?

user13641095
  • 107
  • 6
  • 1
    C doesn't have the standard `string`. Please post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – MikeCAT Jan 06 '21 at 13:26
  • 4
    If you are using CS50, this seems related: [c - What is the difference between char s\[\] and char *s? - Stack Overflow](https://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s) – MikeCAT Jan 06 '21 at 13:26
  • Your code is pretty pointless, you should directly declare an array like `string alphabet = {0,1,2,3,4,...}` or better construct it directly, but anyway it's pretty unclear what you're _actually_ trying to do. "Re-indexing ASCII" isn't very meaningful. This looks like an [XY Problem](http://xyproblem.info/) – Jabberwocky Jan 06 '21 at 13:31
  • 3
    `alphabet[k] = alphabet[k] - 65;` is trying to alter a *string literal* which is read-only. Even if you could, the altered string will be terminated when it finds character 65. Also "has no effect" - it will when you reach letter `'a'`. – Weather Vane Jan 06 '21 at 13:36
  • If the code you show were successful, it would create an array `alphabet` with `alphabet[i]` = `i` for the first 26 elements and `alphabet[26+i]` = `i`+d for the next 26 elements, where d is the difference between the codes for “a” and “A”. This does not seem very useful. It would not be much help in later converting character codes to indices, and it is not even very useful for converting indices to character codes, as there are easier or better ways. I suggest you back away from this particular solution and explain what the larger goal is. – Eric Postpischil Jan 06 '21 at 13:43
  • 1
    Additionally, never use the constant `65` in a program to mean the value of the character “A” unless you are writing a program that works with mixed character encodings, such as ASCII and EBCDIC. To use the code for a particular character in a program, use a character constant, like `'A'` for the code for “A”. This both makes your program more portable and makes the intent more visible to the reader. – Eric Postpischil Jan 06 '21 at 13:45
  • What is the underlying problem you are trying to solve? – August Karlstrom Jan 06 '21 at 14:03
  • Apologises I will be more clear with my underlying problem. I'm trying to encrypt a piece of text, entered by the user, by changing the ASCII value by an integer amount, also supplied by the user. Currently, my code is working to the point where I can encipher the text by the given integer provided the integer doesn't go out of range of the ASCII chart. i.e if the user enters 56, the value of the value will no longer be part of the alphabet. There's a formula to combat this, though it requires that you reindex you ASCII alpha so that 'A' starts at 0 rather than 65 – user13641095 Jan 06 '21 at 17:44

1 Answers1

0
void printAlpha(int i){
    if(i < 26)
        printf("%c", 'A' + i);
    else if(i < 52)
        printf("%c", i - 26 + 'a');
}
Kanony
  • 509
  • 2
  • 12