0

My question is regarding a statement which I am having trouble understanding.

The statement is :

count[str[j]]++

where:

  • count is a count array I took to count each char in a string,
  • str is my given string, and
  • j is loop.

If someone can explain the whole statement that will be good.

gdbj
  • 16,102
  • 5
  • 35
  • 47

2 Answers2

1

Let's break it down. count is getting an element from array count. The index of that element is str[j]. Since j is in a loop, it will change. Finally, ++ is simply incrementing that value of the element in array count. Hope this helps!

Gorgamite
  • 227
  • 2
  • 13
0

Given the count variable, locate the value of the index str[j] where j is the index provided by your iteration. str[j] will return the character of the variable str at index j. So, for instance, if str is "Example", then str[1] is "x". Finally, the statement takes the index of count count["x"] and post-increments it, so that count["x"] is increased by 1

To learn a bit more about post and pre increment, take a look at this answer

Post-increment and Pre-increment concept?

Anthony L
  • 2,159
  • 13
  • 25
  • Oh thank you. Actually I know post increment and Pre-increment where I was stuck was my str[1] is 'x' so if count is my int array how it's index is a char that's where I was getting confused. Because the program is finding the duplicate letter in string using int *count – Waqas Moosa Feb 21 '18 at 01:42
  • `str[1]` is 'x' (a single character, not a string of one character). Then you should use `count['x']` since `str[j]` is a character and not a string. – 1201ProgramAlarm Feb 21 '18 at 01:49