0

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 ?

1
  • 3
    It is calculating the histogram of the input string. array_count is the histogram of s. Commented Aug 20, 2013 at 9:42

3 Answers 3

2

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).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the step by step explanation of the statement. Pretty clear now with the concept.
Note that this is a conceptual explanation, not an actual model of what happens in memory. The compiler might optimize the code differently.
Yes, I get that. This helps in understanding the statement better.
2

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[].

3 Comments

Splendid.That was very elaborate.Clears all my questions.Thanks!
Except without an explicit cast, char is signed by default on the most used platforms... Which means if the string contained any non-ASCII character, it would lead to Undefined Behavior from indexing the array with a negative value.
Is there a different method for checking a string with non-ASCII characters ? What sort of explicit cast would I require ?
0

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



}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.