Demonstrate some basic string operations and mbed online environment limitations.

Dependencies:   mbed

Committer:
CSTritt
Date:
Wed Oct 25 11:40:11 2017 +0000
Revision:
0:5cab3f40a061
Initial version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CSTritt 0:5cab3f40a061 1 /*
CSTritt 0:5cab3f40a061 2 Project: StringTests
CSTritt 0:5cab3f40a061 3 File: main.cpp
CSTritt 0:5cab3f40a061 4 Modified and Ported to mbed/Nucleo by: Dr. C. S. Tritt
CSTritt 0:5cab3f40a061 5 Last revised: 10/22/17 (v. 1.0)
CSTritt 0:5cab3f40a061 6
CSTritt 0:5cab3f40a061 7 Based on Program 6.2 Lengths of strings - from Beginning C, 5th ed. Ivor
CSTritt 0:5cab3f40a061 8 Horton.
CSTritt 0:5cab3f40a061 9
CSTritt 0:5cab3f40a061 10 */
CSTritt 0:5cab3f40a061 11 #include "mbed.h"
CSTritt 0:5cab3f40a061 12 #include "string.h"
CSTritt 0:5cab3f40a061 13
CSTritt 0:5cab3f40a061 14 int main(void)
CSTritt 0:5cab3f40a061 15 {
CSTritt 0:5cab3f40a061 16 // Check for C11 library extensions. It appears that the mbed online
CSTritt 0:5cab3f40a061 17 // compiler does not support the library extensions.
CSTritt 0:5cab3f40a061 18 #if defined __STDC_LIB_EXT1__
CSTritt 0:5cab3f40a061 19 printf("\nOptional functions are defined.\n\n");
CSTritt 0:5cab3f40a061 20 #else
CSTritt 0:5cab3f40a061 21 printf("\nOptional functions are not defined.\n\n");
CSTritt 0:5cab3f40a061 22 #endif
CSTritt 0:5cab3f40a061 23
CSTritt 0:5cab3f40a061 24 // Declare and define strings and count variable.
CSTritt 0:5cab3f40a061 25 char str1[] = "2B or not 2B";
CSTritt 0:5cab3f40a061 26 char str2[] = ",that is not the \0question."; // Manual null insertion!
CSTritt 0:5cab3f40a061 27 unsigned int count; // Used to store the string lengths.
CSTritt 0:5cab3f40a061 28
CSTritt 0:5cab3f40a061 29 // Determine "size" three ways.
CSTritt 0:5cab3f40a061 30 printf("Size of the string \"%s\" is %d characters.\n", str1, sizeof(str1));
CSTritt 0:5cab3f40a061 31 printf("Length of the string \"%s\" is %d characters.\n",
CSTritt 0:5cab3f40a061 32 str1, strlen(str1)); // Danger, not a safe C function.
CSTritt 0:5cab3f40a061 33 for (count = 0; count < sizeof(str1); count++) { // Safely count chars.
CSTritt 0:5cab3f40a061 34 if (str1[count] == '\0') break; // Stop at null char.
CSTritt 0:5cab3f40a061 35 }
CSTritt 0:5cab3f40a061 36 printf("Count of the string \"%s\" is %d characters.\n\n", str1, count);
CSTritt 0:5cab3f40a061 37
CSTritt 0:5cab3f40a061 38 // Repeat for other string.
CSTritt 0:5cab3f40a061 39 printf("Size of the string \"%s\" is %d characters.\n", str2, sizeof(str2));
CSTritt 0:5cab3f40a061 40 printf("Length of the string \"%s\" is %d characters.\n",
CSTritt 0:5cab3f40a061 41 str2, strlen(str2)); // Danger, not a safe C function.
CSTritt 0:5cab3f40a061 42 for (count = 0; count < sizeof(str2); count++) { // Safely count chars.
CSTritt 0:5cab3f40a061 43 if (str2[count] == '\0') break; // Stop at null char.
CSTritt 0:5cab3f40a061 44 }
CSTritt 0:5cab3f40a061 45 printf("Count of the string \"%s\" is %d characters.\n", str2, count);
CSTritt 0:5cab3f40a061 46 }