I want to look for letters of a string and then look-up in the alphabet and then write the index number of alphabet array's index . Like;

A=0
B=1 etc.
c=2
z=25

#
#include "stdafx.h"
#
#include "time.h"
#
#include "stdio.h"
#
#include "string.h"
#
#include "stdlib.h"
#
 
#
#define MAX 80
#
int _tmain(int argc, _TCHAR* argv[])
#
{
#
 
#
        char a[] ="abcdefghijklmnopqrstuvwxyz";
#
char string[MAX];
#
int code[MAX];
#
int len, i, j,
#
printf ("Write your string: ");
#
gets_s(string);
#
len = strlen(string);
#
printf("%s \n", string);
#
 
#
 
#
         for(int i=0;i<len;i++)
#
        for(int j=0;j<25;j++)
#
            if(string[i]==a[j])
#
                                code[i]=j;
#
               
#
}
#
 
#
               
#
       
#
}
#
   
#
        return 0;
#
}

You dont' need that array of the alphabet.

The algorithm is very simple -- just subtract any letter from 'z' and you will have its index. For example, ('Z - 'A' + 1) = (90 - 65 + 1) = 26. You can see how I got those numbers by looking at any ascii chart, such as this one.

So in your program, convert the string to either upper or lower case then do the subtraction in the loop

for(int i = 0; i < len; i++)
{
   cout << string[i] << " = " << 'Z' - string[i] + 1 << '\n';
}

Note that the above will need to be a little more complicated if the string contains non-alphabetical characters, such as numeric digits '0'-'9' or other charactgers.

Edited 6 Years Ago by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.