Solved

string question

Posted on 2004-10-14
1
207 Views
Last Modified: 2010-04-01
I have strings with the following format

"ABSCD 1M" or "GSFDG 3M" or "TTTGF 30Y"

I'm using string class. The 7th character will always be a number. How do I change the strings to show
"ABSCD (1M)" or "GSFDG (3M)" or "TTTGF (30Y)"

Thanks
0
Comment
Question by:healingtao
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
Comment Utility
The idea would be to locate the blank, insert the bracket after it and then append another one to the end, e.g.

#include <string>
#include <iostream>
using namespace std;

string str = "ABSCD 1M";
int pos;

pos = str.find(" ");

if ( -1 == pos) { // not found

    // error
}

str.insert(pos + 1, "("); // insert bracket after blank
str.append(")"); // append other bracket

cout << str.c_str() << endl;

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now