Last active
December 31, 2015 04:09
-
-
Save nootanghimire/7932715 to your computer and use it in GitHub Desktop.
Trim in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<cstring> | |
char* trim(char* str); | |
int main(){ | |
return 1; | |
} | |
char* trim(char* str){ | |
char *ret; | |
int tCa = 0; //totalCharAllocated | |
ret = new char[strlen(str)]; //need to implement the function | |
bool check_first_space = false; | |
int num_spaces_current = 0; | |
while(*str!='\0'){ | |
//iterate through the string | |
if((*str>=65 && *str<=91) || (*str>=97 && *str<=123) | |
||(*str=='+') ||(*str=='-')){ | |
if(!check_first_space) check_first_space = true; | |
num_spaces_current = 0; | |
cout<<*str; | |
*ret = *str; | |
ret++; tCa++; | |
} else if(*str==' '){ | |
num_spaces_current++; | |
if(num_spaces_current==1){ | |
if(check_first_space) | |
cout<<' '; | |
*ret = *str; | |
ret++; tCa++; | |
// need to figure out things | |
} | |
} else { | |
num_spaces_current++; | |
} | |
str++; | |
} | |
char *returning = new char[tCa]; | |
strncpy(returning, ret, tCa); | |
delete []ret; | |
return returning; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment