Solved

string question

Posted on 2004-10-14
1
210 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
ID: 12309462
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

911 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

26 Experts available now in Live!

Get 1:1 Help Now