Charles Tritt / Mbed 2 deprecated StringTests

Dependencies:   mbed

main.cpp

Committer:
CSTritt
Date:
2017-10-25
Revision:
0:5cab3f40a061

File content as of revision 0:5cab3f40a061:

/*
    Project: StringTests
    File: main.cpp
    Modified and Ported to mbed/Nucleo by: Dr. C. S. Tritt
    Last revised: 10/22/17 (v. 1.0)

    Based on Program 6.2 Lengths of strings - from Beginning C, 5th ed. Ivor
    Horton.

*/
#include "mbed.h"
#include "string.h"

int main(void)
{
    // Check for C11 library extensions. It appears that the mbed online 
    // compiler does not support the library extensions.
    #if defined __STDC_LIB_EXT1__
        printf("\nOptional functions are defined.\n\n");
    #else
        printf("\nOptional functions are not defined.\n\n");
    #endif
    
    // Declare and define strings and count variable.
    char str1[] = "2B or not 2B";
    char str2[] = ",that is not the \0question."; // Manual null insertion!
    unsigned int count; // Used to store the string lengths.

    // Determine "size" three ways.
    printf("Size of the string \"%s\" is %d characters.\n", str1, sizeof(str1));
    printf("Length of the string \"%s\" is %d characters.\n", 
        str1, strlen(str1)); // Danger, not a safe C function.
    for (count = 0; count < sizeof(str1); count++) { // Safely count chars.
        if (str1[count] == '\0') break; // Stop at null char. 
    }
    printf("Count of the string \"%s\" is %d characters.\n\n", str1, count);
    
    // Repeat for other string.
    printf("Size of the string \"%s\" is %d characters.\n", str2, sizeof(str2));
    printf("Length of the string \"%s\" is %d characters.\n", 
        str2, strlen(str2)); // Danger, not a safe C function.
    for (count = 0; count < sizeof(str2); count++) { // Safely count chars.
        if (str2[count] == '\0') break; // Stop at null char. 
    }
    printf("Count of the string \"%s\" is %d characters.\n", str2, count);
}