Calculate StringLength in MacOS

This is the very simple example, in this program we will see how to calculate string length in MacOS.

Step 1: Create a Cocoa Application from Mac OS X . Give the application name “StringsLength”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: xpand the Other  Sources folder, then  you can see one file “main.m”. We need to make changes in this file.

Step 4: Open the main.m file and make the following changes in the file.

#import <Cocoa/Cocoa.h>

 int main(int argc, char *argv[])
 {
        char s1[250];
        char s2[250];
        printf("Enter string s1:\n");
        scanf("%s",&s1);
        printf("Enter string s2:\n");
        scanf("%s",&s2);
        if(strlen(s1) == strlen(s2))
        printf("Length of string s1 is equal to length of string s2\n");
        else if(strlen(s1) < strlen(s2))
        printf("Length of string s1 is less than length of string s2\n");
        else
        printf("Length of string s1 is greater than length of string s2\n");
               
        return NSApplicationMain(argc,  (const char **) argv);
}

Step 5: Now compile and run the example and see the output on the console.

You can Download SourceCode from here StringsLength

Leave a Reply

Your email address will not be published. Required fields are marked *