top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Can I capitalize first letter of each word without using arrays? If yes then how?

+3 votes
394 views
Can I capitalize first letter of each word without using arrays? If yes then how?
posted Oct 29, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

Yes of course you can do it.
You have to just play with the ASCII values of letters for example (ASCII value of 'A' is 65 and 'Z' is 90 and for 'a' is 97 and for 'z' is 122).

So here is the trick if you want to convert a capital letter to small then you have to add 32 to that character
and from small to capital then you have to subtract 32 from that character, because of difference of
the ASCII value of ('A' to 'a' is 32 --> [97 - 65 = 32]).

Here is my code (Tested).

#include <stdio.h>

int main()
{
    char str[50] = "this is a simple Example!";
    int i;

    printf("String before change : %s\n", str);

    for (i = 0; str[i]; i++) {
        if ((i == 0) || ((i != 0) && (str[i - 1] == ' '))) {
            if (str[i] >= 'a' && str[i] <= 'z') {
                str[i] -= 32;
            }
        }
    }

    printf("String After change : %s\n", str);

    return 0;
}

Feel free to ask :)

answer Oct 29, 2014 by Arshad Khan
...