Angel
Angel

Reputation: 3

Array element as an index to a different array

I came across some examples where an array is indexed based on values from a different array. Example:

 char s[] = "aloha";
int l= strlen(s);
int array_count[256];
memset(array_count,0,256*sizeof(int));

for(int i=0;i<l;i++)
{
 array_count[s[i]]++;// What exactly happens in this statement ??
}

I understood it as it checking and setting the alphabets in s[] as 1's in the array array_count,which is the alphabet set. Is that right ?

Upvotes: 0

Views: 112

Answers (3)

Bhavesh Shah
Bhavesh Shah

Reputation: 3389

256 is possible letter in string. see the ascii table.

http://www.asciitable.com/

for(int i=0;i<l;i++) 
{ 
     array_count[s[i]]++;    // What exactly happens in this statement ?? 

for i=0

 s[i] = 'a'

ascii value of 'a' is 97

so it will increment arry_count[97] value from 0 to 1



}

Upvotes: 0

verbose
verbose

Reputation: 7917

The code is keeping a histogram of how many times a given character appears in the string. Every time a character appears in the string, the array element corresponding to the ASCII value of that character is incremented by one.

The elements in array_count [] are all set to 0 by your memset(). Then your loop iterates through s[]. So in the first iteration:

array_count [s[i]]++   // first evaluate [i]
array_count [s[0]]++   // i is zero
array_count ['a']++    // s[0] is 'a'
array_count [97]++     // promotion of 'a' from char to int; ASCII value of 'a' is 97

array_count [97] is zero because of the memset, so because of the ++ it gets incremented to 1.

Similar magic happens with the rest of the characters in subsequent iterations; when the loop terminates, array_count [97] will be 2 because of the two 'a's in "aloha"; array_count [0] will be 1 because of the NUL character at the end of "aloha"; and you can figure out what the rest of the array will be (mostly zeros).

Upvotes: 2

Paul Evans
Paul Evans

Reputation: 27577

Each char in s[] has an unsigned int value (usually it's ascii value) inclusively between 0 and 255. array_count[] is initialised to all zeros by the memset. Then, by iterating through s[] from start to end with i in the for loop, the value of each char is used to index into array_count[] and increment it's value with ++. So you get a count of the char values in s[].

Upvotes: 2

Related Questions