fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdbool.h>
  4. #include <ctype.h>
  5.  
  6. bool isAllAlpha(const char *word) {
  7. for (int i = 0; word[i] != '\0'; i++) {
  8. if (!isalpha(word[i])) {
  9. return false;
  10. }
  11. }
  12. return true;
  13. }
  14.  
  15. int main() {
  16. char str[100];
  17. printf("Enter the String:\n");
  18. fgets(str, sizeof(str), stdin);
  19.  
  20. // Remove newline if present
  21. if (str[strlen(str) - 1] == '\n') {
  22. str[strlen(str) - 1] = '\0';
  23. }
  24.  
  25. // Check if entire string is alphabets (and spaces)
  26. for (int i = 0; str[i] != '\0'; i++) {
  27. if (!isalpha(str[i]) && str[i] != ' ') {
  28. printf("ERROR\n");
  29. return 0;
  30. }
  31. }
  32.  
  33. // Process each word
  34. char *ptr = strtok(str, " ");
  35. while (ptr != NULL) {
  36. int len = strlen(ptr);
  37.  
  38. // Convert first and last characters to uppercase
  39. if (len > 0) {
  40. ptr[0] = toupper(ptr[0]);
  41. if (len > 1) {
  42. ptr[len - 1] = toupper(ptr[len - 1]);
  43. }
  44. }
  45.  
  46. ptr = strtok(NULL, " ");
  47. }
  48.  
  49. // Print the final result
  50. printf("%s\n", str);
  51. return 0;
  52. }
Success #stdin #stdout 0s 5320KB
stdin
45
stdout
Enter the String:
ERROR