//* how C-level operations are implemented *
// selected functions from <cstring> library void strcpy(char dest[], const char src[]) { // note: the actual function is not void int i; for(i=0; src[i]!='\0'; i++) dest[i]=src[i]; dest[i]=src[i]; // copy '\0' at the end } void strcat(char dest[], const char src[]) { // note: the actual function is not void int i, j; for(i=0; dest[i]!='\0'; i++) ; for(j=0; src[j]!='\0'; i++, j++) dest[i]=src[j]; dest[i]=src[j]; // copy '\0' at the end } void strcatchr(char dest[], char chr) { int i; for(i=0; dest[i]!='\0'; i++) ; // now we are at the end of the original string dest[i]=chr; i++; // remember about terminating the string! dest[i]='\0'; } int strlen(const char s[]) { int len; for(len=0; s[len]!='\0'; len++) ; return(len); } void strtrunc(char s[], int len) { // first LEN characters are from 0 to (len-1) // and the string needs to be terminated with '\0' s[len]='\0'; } int strcmp(const char s1[], const char s2[]) { // s1<s2 -> -1 // s1>s2 -> +1 // s1==s2 -> 0 int i; for(i=0; s1[i]!='\0' && s2[i]!='\0'; i++) if (s1[i]!=s2[i]) break; // stop comparison if different int r=s2[i]-s1[i]; if (r>0) return(-1); //s1[i]<s2[i], or s1 ended while s2 not else if (r<0) return(+1); //s1[i]>s2[i], or s2 ended while s1 not else /* r==0 */ return(0); //s1[i]==s2[i]=='\0' => both are the same } // Other important functions to remember: // Searching functions // strstr ( string, searchforstr) // strchr ( string, searchforchr)