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.