Counting letters in a string using two for-loops

后端 未结 4 412
臣服心动
臣服心动 2021-01-17 04:45

I have to read the string \"hello world\" and output each letter\'s frequency using only for loops. The instructor hinted that I\'d need to use two loops and ga

4条回答
  •  有刺的猬
    2021-01-17 05:38

    I would use a simple array. Simply convert each letter to an index and increment the array at that index.

    int letters[26];
    int index = ch - 'a';
    letters[index]++;
    

提交回复
热议问题