This code snipplet will find the placement of each letter in a array(string), from another array(character set). For example:
the input would be something like this:
string[256] = "gfdc"
chrset[256] = "abcdefg"
and the output would be:
num[256] = 6, 5, 2 and 3
Find array placement
#include <stdio.h>
#include <string.h>
int main() {
char string[256]; //This will hold out string
char chrset[256]; //this will hold the character set
int num[256]; //This will hold the result
int count = 0; //This is just for loops
printf("Please enter a String: ");
scanf("%s", string);
printf("Please enter a Character set: ");
scanf("%s", chrset); //Get user input for the string & chrset
//This will load the placements into num[]
while(string[count] != 0) {
num[count] = ((strchr(chrset, string[count])) - (int)chrset);
count++; }
count = 0; //This will print out the placements
while(count != strlen(string)) {
printf("%c is in %d\n", string[count], num[count]);
count++; }
printf("Press any key to quit ...\n");
getchar();
getchar();
return 0; }
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.