Find array placement

Hiroshe 0 Tallied Votes 116 Views Share

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

#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; }
mvmalderen 2,072 Postaholic

Your code doesn't even compile :(

Hiroshe 499 Posting Whiz in Training

Wow, how much my programming has canged over a few months! It should compile, but if not, try changing line 17 to:

num[count] = ((strchr(chrset, string[count])) - chrset);

Yeah I know, this isnt really an example of good programming practice, but it was a while ago when I was still learning C. :)

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.