Capitalize in Objective C

Published on 03 October 2020 (Updated: 16 December 2023)

Welcome to the Capitalize in Objective C page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
  NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
  NSString *usage = @"Usage: please provide a string";
  if (argc < 2) {
    printf("%s\n", [usage UTF8String]);
  }
  else {
    NSString* textFromArg = [NSString stringWithUTF8String:argv[1]];
    NSString* normalizedText = [textFromArg stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

    if([normalizedText length] < 1){
      printf("%s\n", [usage UTF8String]);
    }
    else {
      NSString *firstChar = [[normalizedText substringToIndex:1] uppercaseString];
      NSString *remainingText = [normalizedText substringFromIndex:1];
      NSString *capitalizedText = [firstChar stringByAppendingString:remainingText];
      printf("%s\n", [capitalizedText UTF8String]);
    }
  }

  [pool drain];
  return 0;
}

Capitalize in Objective C was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.