Trim Left char tchar[64]

M

muroogan

I read a text file into a char variable

char tchar[64];

I want to trim leading spaces (Left trim).How can I do that in C++.
Any input will be appreciated.
 
M

Mike Wahler

I read a text file into a char variable

char tchar[64];

I want to trim leading spaces (Left trim).How can I do that in C++.
Any input will be appreciated.

#include <algorithm>
#include <iostream>
#include <string>

void trimleft(char *arr)
{
std::string s(arr);
std::string::size_type pos(s.find_first_not_of(" "));

if(pos != std::string::npos)
{
s.erase(0, pos);
s += '\0';
std::copy(s.begin(), s.end(), arr);
}
else
*arr = 0;

}

void show(char *arr, const char *prefix = "")
{
std::cout << prefix << '[' << arr << ']' << '\n';
}

int main()
{
char tchar[64] = " Hello world ";

show(tchar, "Before:\n");
std::cout.put('\n');

trimleft(tchar);
show(tchar, "After:\n");
return 0;
}

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top