I cant see what I'm doing wrong here, I'm trying to write a program with a function that removes the string trailing blanks.
Example: "C++ Programming " would become "C++ Programming"
"Programming is fun " would become "Programing is fun"
using the function
char *(char* str)
Here is what I have so far, but I cant get it to work properly
#include <iostream>
#include <cstring>
using namespace std;
const int Size=80;
char *ptr;
char sentence[Size];
char *dynamicArr;
int j = 0;
int length;
char *clipArray(char array[])
{
length= strlen(array);
for(int i=length-1; array[i]== ' '; i--)
{
if(array[i] == ' ')
{
j++;
}
dynamicArr = new char [length-j];
for (int i = 0; i < length-j; i++)
{
dynamicArr[i] = array[i];
}
}
//cout << "length: " << length << endl;
//cout << "array: " << array << endl;
cout << dynamicArr;
return dynamicArr;
}
int main()
{
cout << "Enter sentence\n";
cin.getline(sentence, Size);
//cout << clipArray(sentence);
char *ptr;
ptr=clipArray(sentence);
while(ptr!=0)
{
//cout << *ptr << endl ;
ptr++;
}
return 0;
}
It seems to be working fine. What are your complaints?
Also, when you post code, put it in [code] tags.