Week 2 project 2 -- use a pointer to play a string (remove spaces before the first word), week 2 --

Source: Internet
Author: User

Week 2 project 2 -- use a pointer to play a string (remove spaces before the first word), week 2 --

/** Copyright (c) 2014, School of Computer Science, Yantai University * All rights reserved. * file name: test. cpp * Author: Liu Chang * Completion Date: July 15, December 13, 2014 * version number: v1.0 ** Problem description: Using pointers as parameters, its core is to implement char * ptrim (char * str) function .; * Input Description: no input required. * program output: output required.


# Include <iostream> using namespace std; char * ptrim (char * str); int main () {char s1 [50] = "Hello world. "; char s2 [50] =" Good morning. "; char s3 [50] =" vagetable bird! "; Cout <" the sorted string is: "<endl; cout <s1 <ptrim (s1) <endl; cout <s2 <ptrim (s2) <endl; cout <s3 <ptrim (s3) <endl; return 0;} char * ptrim (char * str) {int I = 1, j = 0; while (* (str + I + 1 )! = '\ 0') {* (str + (j ++) = * (str + (I ++);} * (str + j) = '\ 0'; return str + I ;}

Running result:



Learning Experience:

In fact, it is still troublesome to do this. It is better to output data directly from the str [1] position, mainly because the question was not clearly read during the previous writing and thought it was to remove all spaces, later, I found that I just needed to remove the leading space.

I am too lazy to change it. It works both in the new array and in the original array, and the memory and efficiency issues are not considered yet. The only doubt is that both str + I and str + j can get the result, but it is strange that str is returned twice.

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.