Answer to Question #34127 in C++ for Kady

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! :)
1
Expert's answer
2013-09-12T12:02:50-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS