Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   String manipulation Problem (http://www.go4expert.com/forums/string-manipulation-problem-t18987/)

katkat21 11Aug2009 14:15

String manipulation Problem
can someone solve my problem

if the string is college
the output must be

it counts that how many a letter does it used
thnk you ^^:)

xpi0t0s 18Aug2009 01:59

Re: String manipulation Problem
I'd probably use an array of 26 ints initialised to zero, then for each letter encountered increment the relevant element. Pseudocode:

int arr[26] initialise to zero
char *str="college"
for each character c in str
end for
for each element e of arr
  if non-zero
    print the letter (e+'a') and the count
  end if
end for

arr[str[c]-'a']++ is the clever bit: str[c] is a char between 'a' and 'z', and subtracting 'a' from that gives us a number from 0-25 which we use as the index into the array. It may be a good idea to add some limits checking to this, for example if (str[c]>='a' && str[c]<='z') so that you don't accidentally increment something not in the array.

All times are GMT +5.5. The time now is 02:19.