LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-07-2015, 09:38 AM   #1
newinubuntu
Member
 
Registered: Jan 2015
Location: India
Distribution: Ubuntu, RHEL
Posts: 37

Rep: Reputation: Disabled
How to add additional character in multiple places of an array string


Hii
I am a beginner of C++ programming environment.
My question:
Quote:
Suppose there is a array..and the contents of the array="ironman"
Now, i need to insert some additional character to this string like "i*ro#n@m^a!n"
I have written a code which concatenate at the end of the string, but i want to concatenate between the string
MY code:
Quote:
#include <iostream>
#include <string>

int main()
{
char in[20] = "ironman";
const unsigned int symbol_size = 5;
std::string symbols[symbol_size];
std::string out(in);
out = out + "@" + "*" + "#";

std::cout << "Output "" << out << ""\n";
}
Output:
Quote:
ironman@*#
Also i need to reversely generate the original contents. That means from "i*ro#n@m^a!n" to "ironman"..

Also i have one constraint, i cannot use much of library function..i can use loop only.

Thanks in Advance !!

Last edited by newinubuntu; 05-07-2015 at 11:46 AM.
 
Old 05-07-2015, 11:12 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well the usual question, what have you done and where are you stuck? You will not learn much if you simply ask for the answer.
 
Old 05-07-2015, 11:37 AM   #3
newinubuntu
Member
 
Registered: Jan 2015
Location: India
Distribution: Ubuntu, RHEL
Posts: 37

Original Poster
Rep: Reputation: Disabled
Unhappy

I m not looking for a readymade codes..i just need the logic..its a usual question..but i have some constraints over it.
Also i am not writing a simple C++..i am going to modify a ns2 module and it just a function. Thats why, i m posting it here.

I updated my post..so kindly check it once..Please i am very new to field..
So help me out !!!

Thankx !!

Last edited by newinubuntu; 05-07-2015 at 11:47 AM.
 
Old 05-08-2015, 01:29 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So you created some variables ... good start. Where is the loop you mentioned to cycle through each character in your array (string) to then allow you to build the new array with the additional
characters?
 
Old 05-08-2015, 08:02 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,642
Blog Entries: 4

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
One thing you'll want to do, right away, is to slurp the values from that character-array (which now contains "ironman") into a true C++ string type. Do your subsequent manipulations using that string. Then, after checking to be sure that the output buffer is large enough to hold the value (and a terminating \0 byte!), transfer the finished string back into that buffer.

If you can't use regular-expressions and so forth, then you'll basically be taking substring-slices from one string, which is your "source," and concatenating them into another string, which will become your "final result." This string will be the one that you copy back into your result buffer, after verifying its size as aforesaid. You do nothing-at-all with that buffer between the time that you slurp the existing value from it and when you pour the result back into it.

The procedure is, "a bit tedious, yes, but entirely straightforward."

(Generally, in C++, there is very little use for "a fixed-size array-of-char." Use the string type, instead. It's faster, more efficient, has no size constraints, can understand Unicode, and so on.)
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading character by character to dynamically allocated array. RollingKeg Programming 3 11-07-2012 07:44 PM
[SOLVED] sed regex get multiple string match in array? ted_chou12 Programming 3 04-09-2011 03:16 AM
[SOLVED] SED assistance: add character in the middle of a string. Vryali Programming 4 10-21-2010 10:51 AM
csh Shell Script: String Concatenation, how do i add a new line character? vxc69 Programming 1 05-04-2009 07:51 PM
C++ string object; add whitespace controll character sureshkellemane Programming 5 01-13-2009 06:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration