LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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, 03:20 PM   #1
newinubuntu
Member
 
Registered: Jan 2015
Location: India
Distribution: Ubuntu, RHEL
Posts: 37

Rep: Reputation: Disabled
How to assign null values to an array in C++


I wrote a program which concatenate additional character to a string in c++.

Code:
#include <iostream>
#include <string>
#include <cstring>

std::string interchange_merge( const char *s1, const char *s2 )
{
    std::string result;
    result.reserve( std::strlen( s1 ) + std::strlen( s2 ) );

    while ( *s1 && *s2 )
    {
        result.push_back( *s1++ );
        result.push_back( *s2++ );      
    }

    while ( *s1 ) result.push_back( *s1++ );
    while ( *s2 ) result.push_back( *s2++ );

    return result;
}

int main()
{
    char in[] = "ironman";
    char symbols[] = "*%#@^!";

    std::string s = interchange_merge( in, symbols );

    std::cout << s << std::endl; 
}
Output:
Quote:
i*r%o#n@m^a!n
So..now i want to write the reverse function of the above code..
That means,
INPUT:
Quote:
i*r%o#n@m^a!n
"*%#@^!" values should be null.
OUTPUT:
Quote:
ironman
Thankx in advance !!
 
Old 05-07-2015, 03:29 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,948

Rep: Reputation: 1421Reputation: 1421Reputation: 1421Reputation: 1421Reputation: 1421Reputation: 1421Reputation: 1421Reputation: 1421Reputation: 1421Reputation: 1421
I don't get the 'values should be null' part, but if you want to remove every other character, then just do that:
Code:
void every_odd_char (const char *from, char *to)
{
    const char *p= from;
    const char *plim;
    char *q= to;

    if (!p) goto RETURN;
    for (plim= p + strlen (p); p<plim; p+=2) {
        *q++ = *p;
    }

RETURN:
    *q= '\0';
}
 
1 members found this post helpful.
Old 05-07-2015, 03:38 PM   #3
newinubuntu
Member
 
Registered: Jan 2015
Location: India
Distribution: Ubuntu, RHEL
Posts: 37

Original Poster
Rep: Reputation: Disabled
Dear NevemTeve,

I am not getting very well..
If you don't mind..can you please put one function on my code which will do this task.
i apologize you because i know that asking for code is a invalid post..but i have no choice..please help me out !!
 
Old 05-08-2015, 02:07 AM   #4
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741Reputation: 741
do you mean like this?

Code:
#include <iostream>
#include <string>
#include <cstring>

std::string interchange_merge( const char *s1, const char *s2 )
{
    std::string result;
    result.reserve( std::strlen( s1 ) + std::strlen( s2 ) );

    while ( *s1 && *s2 )
    {
        result.push_back( *s1++ );
        result.push_back( *s2++ );      
    }

    while ( *s1 ) result.push_back( *s1++ );
    while ( *s2 ) result.push_back( *s2++ );

    return result;
}

std::string everySecond(const std::string& in)
{
  std::string ret ;
  
  for (size_t i = 0 ; i < in.size() ; i+=2)
  {
    ret.push_back(in[i]) ;

  }

  return ret;
}



int main()
{
    char in[] = "ironman";
    char symbols[] = "*#@^!";


    std::string s = interchange_merge( in, symbols );

    std::cout << s << std::endl; 

    std::string s1 = everySecond(s);
    std::cout << s1 << std::endl; 
    
    size_t len = s.size() - s1.size() ;
    
    std::string s2(len, '0') ; // adopt to required
    std::cout << s2 << std::endl; 
}

i*r%o#n@m^a!n
ironman
000000
 
1 members found this post helpful.
Old 05-08-2015, 09:04 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179
You see, one thing to be aware of ... and to fear ... about char foo[] = "bar";, is that you (only) get exactly enough bytes to store the value that you've specified. Very easy to scribble-over memory that way. Use strings. Use the actual string type.
 
Old 05-08-2015, 01:35 PM   #6
newinubuntu
Member
 
Registered: Jan 2015
Location: India
Distribution: Ubuntu, RHEL
Posts: 37

Original Poster
Rep: Reputation: Disabled
Thumbs up

Dear a4z,
Thank you very much

Problem is solved.

I code it in another way just take a look:
Code:
#include <iostream>
#include <algorithm>
#include <string>

bool IsParenthesesOrDash(char c)
{
    switch(c)
    {
        case '~':  //~<@#$%^&*_+|:?!<
		case '<':
		case '@':
		case '#':
		case '$':
		case '%':
		case '^':
		case '&':
		case '*':
		case '_':
		case '+':
		case '|':
		case ':':
		case '?':
		case '!':
		case '>':
        return true;
    default:
        return false;
    }
}

int main()
{
    std::string str("1~9<2@.#3$.%0^.&1*2_8+|:?!<");
    str.erase(std::remove_if(str.begin(), str.end(), &IsParenthesesOrDash), str.end());
    std::cout << str << std::endl;
}
Quote:
output:192.3.0.128

Last edited by newinubuntu; 05-08-2015 at 01:39 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
assign vaiables from string values tsdjim Programming 3 03-15-2013 04:19 AM
PHP Array to Tuple (Part 2): Conserving Null Values rm_-rf_windows Programming 2 04-25-2012 10:54 AM
[SOLVED] reference bash array values, using a variable with a value of the array name gusthecat Programming 5 03-07-2012 04:41 PM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 10:38 PM
Cannot assign NULL to array? exvor Programming 7 10-31-2006 02:00 PM

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

All times are GMT -5. The time now is 01:23 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration