Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 11:56

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



STRING PROBLEM!

Discussion on STRING PROBLEM! within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Dec 2011
Posts: 367
Received Thanks: 199
STRING PROBLEM!

Hi community,
i have a problem whit the string in c++ !

Example:

string data("1234567890");

to keep the number 1 and delete numer 2?

Result: 13579

from the first to the end!

help me please ...
Kingrap is offline  
Old 04/20/2012, 21:06   #2
 
omer36's Avatar
 
elite*gold: 0
Join Date: Mar 2009
Posts: 2,317
Received Thanks: 1,255
Code:
string data = "1234567890";


for (int i=1; i< data.size(); i += 2){
    cout << i << endl;
}
so?
omer36 is offline  
Thanks
1 User
Old 04/20/2012, 21:16   #3


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,902
Received Thanks: 25,407
Quote:
Originally Posted by omer36 View Post
Code:
string data = "1234567890";


for (int i=1; i< data.size(); i += 2){
    cout << i << endl;
}
so?
you just give them out, you dont save them to the string
MrSm!th is offline  
Thanks
1 User
Old 04/20/2012, 21:18   #4
 
elite*gold: 0
Join Date: Dec 2011
Posts: 367
Received Thanks: 199
So? No sorry..

No..

Example:

string data = "19A546D7482250A1R6T";

-------------------------------------------------------------------------------------
the data is random! xD
-------------------------------------------------------------------------------------

RESULT: 1A4D425ART

-------------------------------------------------------------------------------------
one yes, one no, one yes, one no..
-------------------------------------------------------------------------------------

Help me please
Kingrap is offline  
Old 04/20/2012, 21:45   #5


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,902
Received Thanks: 25,407
Quote:
Originally Posted by Kingrap View Post
No..

Example:

string data = "19A546D7482250A1R6T";

-------------------------------------------------------------------------------------
the data is random! xD
-------------------------------------------------------------------------------------

RESULT: 1A4D425ART

-------------------------------------------------------------------------------------
one yes, one no, one yes, one no..
-------------------------------------------------------------------------------------

Help me please
thats exactly what he wrote, just without saving it to another string

Code:
string result;
result.reserve(data.capacity()/2 + 1);

for(int i=0; i<data.length(); i += 2)
    result.push_back(data[i]);
MrSm!th is offline  
Thanks
1 User
Old 04/20/2012, 22:05   #6
 
omer36's Avatar
 
elite*gold: 0
Join Date: Mar 2009
Posts: 2,317
Received Thanks: 1,255
Ach, lol was fürn fail hab ich denn da geschrieben, das ging nur zufällig gut, weil der String von 1--9 ging...Und ich einfach nur aus der Schleife jede 2. Zahl ausgegeben habe..


Like this?

Code:
#include <iostream>
#include <conio.h>

using namespace std;

int main(){

    string data = "A12BC34E56D4S";
    char split[data.size()];

    for (int i=0; i<data.size(); i+=2){
        split[i] = data.at(i);
    }

    for (int i=0; i<sizeof(split); i ++){
        cout << split[i];
    }


    _getch();

}
omer36 is offline  
Thanks
1 User
Old 04/20/2012, 22:29   #7


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,902
Received Thanks: 25,407
Das ist noch größerer Käse.
1. ist es Speicherplatzverschwendung den neuen String so groß wie den alten zu machen
2. ist jedes 2. Zeichen ein random Zeichen bei dir, weil du i als Index für den Ziel Buffer nimmst

Warum du ein char Array anstatt eines Strings nimmst, ist mir nicht ganz klar und at() verbraucht nur unnötig Laufzeit, weil es den Index prüft. Da du in der Schleife ohnehin abbrichst, wenn du das Ende erreicht hast, kannst du den ungeprüften [] Operator nutzen.

Ich würde übrigens jemanden mit besserem Wissen über die STL als ich es habe bitten eine weitere Version zu machen, vielleicht mit Iteratoren.
Meine kommt mir so unschön vor, hab aber auf die Schnelle nix anderes an String Methoden für dieses Vorgehen gefunden und ich war mir nicht sicher, ob Iteratoren mit +=2 oder dem % Operator umgehen können.
MrSm!th is offline  
Thanks
1 User
Old 04/20/2012, 23:35   #8
 
elite*gold: 0
Join Date: Dec 2011
Posts: 367
Received Thanks: 199
Perfect!! Thanks friends!

Code:
string result;
result.reserve(data.capacity()/2 + 1);

for(int i=0; i<data.length(); i += 2)
    result.push_back(data[i]);
Now i have another problem ,

i got hex data of the string (im woking whit a password of a game xD)..

RESULT = 74657374

that is : 74 65 73 74
t e s t

-----------------------------------------
HEX TO CHAR
-----------------------------------------


how to convert hex to char of the "RESULT" string?

Example:

string hex = "74657374"

RESULT = test..

after this i haven't other questions .

Help me please, Thanks again!

-----------------------------------------
CHAR AND HEX TABLE :
Kingrap is offline  
Old 04/21/2012, 00:44   #9


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,902
Received Thanks: 25,407
I dont understand your problem.
Every character is just represented by a numerical value.

Just convert your Byte Array of hex data into a char array and initialize a string with it and you are done:

Code:
byte pw[] = { 0x74, 0x65, 0x73, 0x74 };

char *cstr = new char[sizeof(pw)+1];
memcpy(cstr, pw, sizeof(pw));
cstr[sizeof(pw)] = '\0';

string str(cstr);

//done
MrSm!th is offline  
Old 04/21/2012, 10:09   #10
 
Tyrar's Avatar
 
elite*gold: 0
Join Date: Oct 2008
Posts: 1,637
Received Thanks: 1,119
Quote:
Originally Posted by MrSm!th View Post
Ich würde übrigens jemanden mit besserem Wissen über die STL als ich es habe bitten eine weitere Version zu machen, vielleicht mit Iteratoren.
Meine kommt mir so unschön vor, hab aber auf die Schnelle nix anderes an String Methoden für dieses Vorgehen gefunden und ich war mir nicht sicher, ob Iteratoren mit +=2 oder dem % Operator umgehen können.
iteratoren müssten genau so mit +=2 umgehen können wie fast alles andere auch

Code:
std::string result;
result.reserve(data.capacity()/2+1);
for(std::string::const_iterator it=data.begin(); it!=data.end(); it+=2)
result.push_back(*it);
müsste also funktionieren

Quote:
Originally Posted by MrSm!th View Post
Code:
byte pw[] = { 0x74, 0x65, 0x73, 0x74 };

char *cstr = new char[sizeof(pw)+1];
memcpy(cstr, pw, sizeof(pw));
cstr[sizeof(pw)] = '\0';

string str(cstr);

//done
ich glaub er meinte von einem hex string in einen normalen string zu konvertieren: std::string("74657374") soll am ende "test" ergeben.
ich würde das ganze auf anhieb so probieren:
Code:
std::string result;

for(uint32_t i=0; i<data.size(); ++i) {
    char c1[2]={0};
    c1[0]=data[i];
    char c2[2]={0};
    c2[0]=data[++i]; // i zuerst erhöhen, dann verwenden? bin mir grad nich sicher
    char b[2]={0};
    b[0]=(strtol(c1,nullptr,16)<<4)|strtol(c2,nullptr,16);
    result.append(b);
}
// untested!
das ganze nicht getestet, lässt sich eher als pseudo code lesen
Tyrar is offline  
Thanks
1 User
Old 04/21/2012, 11:21   #11


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,902
Received Thanks: 25,407
Wäre für mich sehr seltsam, wenn das Passwort in Hex Zeichen als String Literal dargestellt würde oO

Und was soll dieser komplizierte Code? :O Viel zu viel C :/

Code:
string result;
result.reserve(data.capacity()/2 + 1);

for(int i=0; i<data.length(); i += 2)
{
    string tmp = data.substr(i, i+1);
    char c = strtol(tmp.c_str(), nullptr, 0);
    result.push_back(c);
}
Ginge vermutlich noch leichter und ohne strtol mithilfe eines stringstreams.
MrSm!th is offline  
Thanks
1 User
Old 04/21/2012, 14:20   #12
 
elite*gold: 0
Join Date: Dec 2011
Posts: 367
Received Thanks: 199
ich glaub er meinte von einem hex string in einen normalen string zu konvertieren: std::string("74657374") soll am ende "test" ergebe

yes, hex string to normal string..

std::string("74657374");
the normal string must be : test..
the number: 74 65 73 74 are hex..

sorry for my english but im italian

HeavyHacker there is a error in the code..
MrSm!th there is a error in the code..

my code is..

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

using namespace std;

int main(){

	string password("0E4S7T4C6D50743C7O4");

	if (password.length()%2 == 0) {
		string password4(password.substr(4,password.length()));
		string result;
		result.reserve(password4.capacity()/2 +1);
		for(int i=0; i<password4.length(); i += 2)
		result.push_back(password4[i]);
		cout << "Password hex: " << result;
	} else {
		string password3(password.substr(3,password.length()));
		string result;
		result.reserve(password3.capacity()/2 +1);
		for(int i=0; i<password3.length(); i += 2)
		result.push_back(password3[i]);
		cout << "Password hex: " << result;
	}

	getchar();
}
Help me please!
Kingrap is offline  
Old 04/21/2012, 21:56   #13


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,902
Received Thanks: 25,407
But S is not a valid hex digit oO

Quote:
yes, hex string to normal string..

std::string("74657374");
the normal string must be : test..
the number: 74 65 73 74 are hex..
that would be
Code:
string result;
result.reserve(data.capacity()/2 + 1);

for(int i=0; i<data.length(); i += 2)
{
    string tmp = data.substr(i, i+1);
    char c = strtol(tmp.c_str(), nullptr, 0);
    result.push_back(c);
}
what "error" do you mean?
MrSm!th is offline  
Thanks
1 User
Old 04/21/2012, 21:59   #14
 
elite*gold: 0
Join Date: Dec 2011
Posts: 367
Received Thanks: 199
nullptr is error
Kingrap is offline  
Old 04/21/2012, 22:32   #15
 
elite*gold: 5
Join Date: Sep 2006
Posts: 385
Received Thanks: 218
Then you're using an old compiler. Replace it with 0 or NULL.
Nightblizard is offline  
Thanks
1 User
Reply


Similar Threads Similar Threads
C++ String Problem
04/05/2012 - C/C++ - 6 Replies
Ich stehe vor einem blöden problem, wahrscheinlich ist die Lösung ziemlich simple aber habe keine Idee wie ich das umsetzen soll undzwar habe ich einen std::string mystring "000111110000110010111100" und ich möchte dazwischen / dahinter die length einfügen so das , dass wie folgt aussieht: length 3 (000) length 5(11111) length 4 (0000) length 2(11) length 2 (00) length 1 (1) length 1 (0) length 4(1111) length 2(00) am ende sollte der string so aussehen: ...
String problem
10/03/2011 - CO2 Programming - 9 Replies
Is not much of a conquer problem, but more a programming problem, but it's related to Conquer as it's for an ani editor. So the problem is, when I'm reading the ids of the files, then the result is something like this: 561359 1 I have tried split with \n and check if the current char was alpha only, but it still does it :/
[VB08]String in String mit mehreren Funden
08/08/2011 - .NET Languages - 6 Replies
Hey, bin gerade auf ein Problem gestoßen, an dem ich mir seit 3 Stunden die Zähne ausbeiße. Ich will eine Funktion schreiben, die der _StringBetween Funktion von AutoIt gleich ist. _StringBetween gibt in einem Array alle Strings zwischen zwei SubStrings und dem ganzen String aus. Die Ausgabe bei _StringBetween("<h1>test1</h1>&l t;h1>test2</h1>", "<h1>", "</h1>") wäre also idealer Weiße ein Array (x = "test1", x = "test2")... da man in VB08 kein Array returnen kann, komme ich aber einfach...
[C++] string zwischen string
11/11/2010 - C/C++ - 6 Replies
tag gibts direkt ne funktion, mit der man einen passenden string zwischen dem string suchen kann? also meine net .find() sondern sowas ähnliches, die in diesem beispiel "mein string sucht" Bsp: "<span id=\"lalala\">"+string mein_string+"</span>" understanden? :-)
String.au3
09/11/2010 - AutoIt - 2 Replies
Hey, hat jemand die Datei für mich? Ich finde im Internet nichts (ich hoffe ich habe nichts übersehn) Mfg



All times are GMT +1. The time now is 11:57.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.