Charles Tritt / Mbed 2 deprecated StringTests

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
CSTritt
Date:
Wed Oct 25 11:40:11 2017 +0000
Commit message:
Initial version.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Oct 25 11:40:11 2017 +0000
@@ -0,0 +1,46 @@
+/*
+    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);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Oct 25 11:40:11 2017 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/b484a57bc302
\ No newline at end of file