How to remove the first word from string in C++

2 Answers

0 votes
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

int main()
{
	string s = "c++ c c# java php";

	s = s.substr(s.find_first_of(" \t") + 1);

	cout << s << endl;

	return 0;
}


/*
run:

c c# java php

*/

 



answered Apr 19, 2018 by avibootz
0 votes
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

// Function to remove the first word (up to first space or tab)
string TrimFirstWord(const string& input) {
    size_t pos = input.find_first_of(" \t");
    
    if (pos != string::npos)
        return input.substr(pos + 1);
        
    return input; // return original if no space or tab found
}

int main()
{
    string s = "c++ c c# java php";

    s = TrimFirstWord(s);

    cout << s << endl;

    return 0;
}


/*
run:

c c# java php

*/

 



answered Sep 25, 2025 by avibootz
...