Theo Ekelmans
/
substring_demo
Simple substring demo using only the MBED default library
Diff: main.cpp
- Revision:
- 0:97a47026da17
diff -r 000000000000 -r 97a47026da17 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Jan 10 21:58:57 2012 +0000 @@ -0,0 +1,46 @@ +// Simple substring demo using only the default MBED library +// Theo@Ekelmans.com (NL) + +#include "mbed.h" + +Serial usbUART ( USBTX, USBRX ); + +void substring(char *s,char *d,int pos,int len) { +//usage: substring(Source,Destination,pos,len); + + char *t; + + s=s+(pos-1); + t=s+len; + while (s!=t) { + *d=*s; + s++; + d++; + } + *d='\0'; +} + + +int main() { + usbUART.baud(115200); + usbUART.printf("%c", 12); + + char Source[50]="working with strings is fun 0123456789 9876543210"; + char Destination[50]; + int pos,len; + + usbUART.printf("----------------------------------------------------------\r\n"); + usbUART.printf(" substring demo \r\n"); + usbUART.printf("----------------------------------------------------------\r\n"); + usbUART.printf("source: %s\r\n", Source); + usbUART.printf("----------------------------------------------------------\r\n"); + printf("input the start position, tap enter, input length, tap enter "); + scanf("%d%d",&pos,&len); + printf("\n\n"); + + substring(Source,Destination,pos,len); + + printf("s= %s\n\r", Source); + printf("d= %s\n\r", Destination); + +}