Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_STR_NUM 64
- #define MAX_STR_LEN 49
- char* nextLine() {
- char* result;
- char* string = malloc(sizeof(char)*(MAX_STR_LEN));
- int position = 0;
- do
- {
- string[position] = getchar();
- position++;
- } while ((position < MAX_STR_LEN) && (string[position-1] != '\n'));
- string[position-1] = '\0';
- result = malloc(sizeof(char)*(position+1));
- strcpy(result,string);
- free(string);
- return result;
- }
- void trimLeading(char *string) {
- int length = strlen(string);
- int i;
- for(i = 0; string[i] == ' '; i++) {}
- if(i != 0){
- for(int j = i; j < length; j++){
- string[j-i] = string[j];
- string[j] = ' ';
- }
- }
- }
- void trimMiddle(char *string) {
- int length = strlen(string);
- char* temp = malloc(sizeof(char)*length);
- int counter = 0;
- for(int i=0; i<length; i++){
- if(string[i] != ' '){
- temp[counter] = string[i];
- counter++;
- } else if(string[i+1] != ' ') {
- temp[counter] = string[i];
- counter++;
- }
- }
- strcpy(string,temp);
- free(temp);
- }
- void trimTrailing(char *string) {
- int length = strlen(string);
- if(length != 0){
- for(int i = length-1; string[i] == ' '; i--){
- string[i] = '\0';
- }
- }
- }
- void trim(char *string) {
- trimMiddle(string);
- trimLeading(string);
- trimTrailing(string);
- }
- int main(){
- char *pointers[MAX_STR_NUM];
- char *string = malloc(sizeof(char)*MAX_STR_LEN);
- int counter = 0;
- do{
- printf("Enter a string: ");
- string = nextLine();
- trim(string);
- pointers[counter] = malloc(sizeof(char)*strlen(string));
- strcpy(pointers[counter],string);
- printf("User entered \"");
- printf(pointers[counter]);
- printf("\"\n");
- counter++;
- } while(counter < MAX_STR_NUM && strlen(string) != 0);
- free(string);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement