Question #34127

how am i supposed to remove all leading and trailing whitespaces from an array? please give me some logic so i can work on it! :)

Expert's answer

#include <cctype>
#include <stdio.h>
#include <cstdlib>
#include <string.h>
#include <iostream>


using namespace std;


const int N = 100;


char array[N];
char arr[N];


int main () {
gets(array);
strcpy(arr, array);
int k = 0;
int array_length = strlen(array);
while (k < array_length && isspace(arr[k])) ++k;//find number of white spaces
for (int i = k; i <= array_length; i++)
arr[i - k] = arr[i];
printf("%s
", arr);
cin.get();
return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS