C program to capitalize the first and last letter of each word in the given string. The input necessary for this is a string whose first and last letters are to be capitalized. Our desired output is the string after capitalizing the first and last letter of each word in the string.
To read our input string (c) at runtime, we can make use of predefined gets() function. This function reads one line of input everytime and stores it in a string pointer or character array. It always reads until it reaches either the end or encounters newline.
char c[150];
printf(“Enter a string : “);
gets(c);
We will now take two variables to capitalize first and last characters for storing its index position i.e., f and l respectively. Initially f is zero and we iterate from first index (0) to the last character (n). ‘n’ can be calculated by using predefined function strlen() from the “string.h” library.
n=strlen(c);
f=0;
As we iterate, if we encounter a space (‘ ‘) then, the previous index (i-1) is stored in l. If both f and l are equal then, there is only one character and we have to capitalize it once (c[l]=c[l]-32). To capitalize a character we have to subtract 32 from it.
This is because for uppercase letters the value is between 65-90 whereas for lowercase it is 97-122. Hence, by subtracting it 32 we gets its corresponding uppercase letter. If both f and l are different then, we capitalize it in a similar manner separately.
After this, we update the f position to i+1 to repeat the same for the next word in the string.
for(i=0;i<n; ++i) {
if(c[i]==’ ‘) {
l=i-1;
if(l==f) {
c[l]=c[l]-32;
}
else {
c[l]=c[l]-32;
c[f]=c[f]-32;
}
f=i+1;
}
}
After reaching the end of string we will still be left with capitalizing the last word’s both first and last character. So, for the last word the l will be i-1 whereas f is already update. We capitalize both by subtracting by 32.
c[i-1]=c[i-1]-32;
c[f]=c[f]-32;
Now, the final updated value of the character array or string (c) is nothing but, our desired resultant output whose, first and last character of each word is capitalized. This same string is displayed on the output console screen using printf() function.
printf(“String after removal : %s”,c);
Capitalize the First and Last Letter of Each Word of A String in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <stdio.h> #include <string.h> int main() { char c[150]; int i,f,l,n; printf("Enter a string : "); gets(c); n=strlen(c); f=0; for(i=0;i<n; ++i) { if(c[i]==' ') { l=i-1; if(l==f) { c[l]=c[l]-32; } else { c[l]=c[l]-32; c[f]=c[f]-32; } f=i+1; } } c[i-1]=c[i-1]-32; c[f]=c[f]-32; printf("String after removal : %s",c); |
Output:
1 2 |
Enter a string : hello world welcome to semleap String after removal : HellO WorlD WelcomE TO SemleaP |
Capitalize The First and Last Letter of word – Using Function
In the above type we can see that, the entire code is within main method only. The disadvantage with it is that, the code is not reusable and not very readable either. Instead, if we split the code according to its logic and functionality and use functions to place parts of code separately.
This way, when same logic is to be used elsewhere in the code then, we can just make the function call instead of rewriting the code and make it reusable. Also, the code becomes a lot cleaner and finding some part is lot easier and hence, makes it readable.
So, the main method first reads the input string using gets() function. Then, it passes this as parameter to the user-defined function(change) which has the same logic as above to capitalize the first and last character of every word in the parameter passed.
This modified input string in now our desired output whose first and last character of every word is capitalized. The same is displayed on the output console screen using printf().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <stdio.h> #include <string.h> void change(char *c) { int i,f,l,n; n=strlen(c); f=0; for(i=0;i<n; ++i) { if(c[i]==' ') { l=i-1; if(l==f) { c[l]=c[l]-32; } else { c[l]=c[l]-32; c[f]=c[f]-32; } f=i+1; } } c[i-1]=c[i-1]-32; c[f]=c[f]-32; } int main() { char c[150]; printf("Enter a string : "); gets(c); change(c); printf("String after removal : %s",c); return 0; } |
Output:
1 2 |
Enter a string : java tutoring String after removal : JavA TutorinG |