String truncation instance

Source: Internet
Author: User

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 ;}

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.