String truncation instance
# Include
// Return the address of a intercepted string through the pointer function char * substring (char s [], int I, int j) {// The temporary array must be static, otherwise, static char temp [100]; int n, m; for (m = 0, n = I; n <= j; n ++, m ++) is not returned) {temp [m] = s [n];} temp [m] = '\ 0'; return temp;} int main () {char str [] = "I Love Programming"; char * ps = NULL; ps = substring (str, 2, 5); printf ("% s \ n", ps ); return 0 ;}
// Version 2 # include
Char * substring (char s [], char temp [], int I, int j) {// static char temp [100]; // The local variable int n is no longer defined, m; for (m = 0, n = I; n <= j; n ++, m ++) {temp [m] = s [n];} temp [m] = '\ 0'; return temp;} int main () {char str [] = "I Love Programming"; char temp [100]; // define an array for storing the truncated string // substring (str, temp, 2, 5); printf ("% s \ n", temp); return 0 ;}