Experimentation with character arrays (also known as strings).
Dependencies: mbed
Revision 0:eeacb336ebf6, committed 2017-11-02
- Comitter:
- rossatmsoe
- Date:
- Thu Nov 02 18:58:43 2017 +0000
- Commit message:
- Experimentation with character arrays (also known as strings).
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 Thu Nov 02 18:58:43 2017 +0000 @@ -0,0 +1,70 @@ +#include "mbed.h" + +// Fun with strings! + +Serial pc(USBTX,USBRX); +void yell(char *phrase); +void cartoon_curse(int length); + + +int main() { + + char message[]="Hello World!"; + + pc.printf("%s\n",message); + pc.printf("%c\n",message[4]); + pc.printf("%d\n",message[4]); + yell(message); + yell("la la la"); + cartoon_curse(4); + + while(1) {} +} + + // Function that will take a string (char array) as input + // and print that string in all caps + void yell(char *phrase) { + int k=0; + while(phrase[k]!=0){ + if( (phrase[k]>=97) && (phrase[k]<='z') ) { + pc.printf("%c",phrase[k]-32); + } + else { + pc.printf("%c",phrase[k]); + } + k++; + } + pc.printf("!!!\n"); +} + +// Function that will take random characters from an +// array of symbols to make an expression used by +// angry cartoon characters +void cartoon_curse(int length) { + char good_symbols[]="!@*#&$%?"; + for(int k=0; k<length; k++) { + int n=rand()%8; + pc.printf("%c",good_symbols[n]); + } + pc.printf("!\n"); +} + + + + + + + + + + + + + + + + + + + + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Nov 02 18:58:43 2017 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/fb8e0ae1cceb \ No newline at end of file