Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a wchar_t* in my C code, that contains URL in the following format:
https://example.com/test/...../abcde12345
I want to split it by the slashes, and get only the last token (in that example, I want to get a new wchar_t* that contains "abcde12345").
How can I do it?
Thank you?
In C, you can use wcsrchr to find the last occurence of /:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
int main(void){
wchar_t *some_string = L"abc/def/ghi";
wchar_t *last_token = NULL;
wchar_t *temp = wcsrchr(some_string, L'/');
if(temp){
temp++;
last_token = malloc((wcslen(temp) + 1) * sizeof(wchar_t));
wcscpy(last_token, temp);
}
if(last_token){
wprintf(L"Last token: %s", last_token);
}else{
wprintf(L"No / found");
}
}
Just use std::wstring.
int main()
{
std::wstring input = L"https://example.com/test/...../abcde12345";
auto separatorPos = input.rfind(L'/');
if (separatorPos == std::wstring::npos)
{
std::cout << "separator not found\n";
}
else
{
auto suffix = input.substr(separatorPos + 1);
std::wcout << suffix << L'\n';
}
return 0;
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
Let’s say I have 128 and I broke it up into 1, 2 and 8.
Can anyone give me a logic to build the number again from its broken digits.
If you have a vector of ints, you can do:
#include <vector>
#include <cstdio>
#include <cstdlib>
int to_integer( const std::vector<int>& v ) {
int number = 0;
for ( int value : v ) {
number = 10*number + value;
}
return number;
}
int main() {
std::vector<int> vec {1,2,8};
int number = to_integer( vec );
printf( "Number:%d\n", number );
return 0;
}
If it was a string, you could simply use the C library
#include <cstdlib>
...
const char* str = "128";
int number = ::atoi( str );
But that's likely not what you are asking.
You can do the hands-on approach
#include <cstdio>
int to_integer( const char* str ) {
int number = 0;
for ( ; *str != '\0'; str++ ) {
char ch = *str;
number = 10*number + (ch-'0');
}
return number;
}
int main() {
const char* str = "128";
int number = to_integer( str );
printf( "Number:%d\n", number );
return 0;
}
Please note that this routine above is just a minimal, simplistic and it does not check for cases that the C library does as eg: non-numeric charaters, white spaces, null pointer.
However many times we can guarantee all above as a precondition so the above becomes valid production code. I actually use something like that for high speed trading.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
How could I convert the following string:
std::string str = "x89x30x50";
to this following unsigned char array/byte array with escape sequences:
unsigned char char_arr[1024] = "\x89\x30\x50";
If I understand your question correctly you want to split a string of hexadecimal values (prefixed by 'x') into byte values.
You could achieve this by tokenizing the string (with a delimiter of 'x') and converting each token to the byte value:
Something like this:
#include <string>
#include <sstream>
int main()
{
std::string str = "x89x30x50";
unsigned char char_arr[1024] = "";
std::istringstream iss(str);
std::string token;
int i = 0;
while (std::getline(iss, token, 'x'))
{
if (token.empty()) continue;
char_arr[i++] = static_cast<unsigned char>(std::stoi(token, nullptr, 16));
}
//char_arr = {0x89,0x30,0x50,0,0,...}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need to create a function to check if the string I send as a parameter has the first 4 characters as a letter, and the last 3 are digits, and that it has exactly 7 characters. How would I code this function?
The simplest solution would be to loop through the string checking each individual character, eg:
#include <string>
#include <cctype>
bool is4LettersAnd3Digits(const std::string &s)
{
if (s.length() != 7)
return false;
for (int i = 0; i < 4; ++i) {
if (!std::isalpha(s[i]))
return false;
}
for (int i = 4; i < 7; ++i) {
if (!std::isdigit(s[i]))
return false;
}
return true;
}
Alternatively:
#include <string>
#include <algorithm>
#include <cctype>
bool is4LettersAnd3Digits(const std::string &s)
{
return (
(s.length() == 7) &&
(std::count_if(s.begin(), s.begin()+4, std::isalpha) == 4) &&
(std::count_if(s.begin()+4, s.end(), std::isdigit) == 3)
);
}
Alternatively, if using C++11 or later:
#include <string>
#include <algorithm>
#include <cctype>
bool is4LettersAnd3Digits(const std::string &s)
{
if (
(s.length() == 7) &&
std::all_of(s.begin(), s.begin()+4, std::isalpha) &&
std::all_of(s.begin()+4, s.end(), std::isdigit)
);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this function:
void map()
{
map<char, string> change;
string usrstr = "A APPLE AND BANANA";
change['A'] = "00011";
change['B'] = "11001";
change['C'] = "01110";
change[' '] = "$$";
}
How would I go about changing all occurrences of 'A' in my string to "00011" and the same for B, C and space. All help is much appreciated
P.S The string wont always be the same
Not sure to understand: how about:
std::string str = "A APPLE AND BANANA";
std::replace(str.begin(), str.end(), "A", "00011" );
std::replace(str.begin(), str.end(), "B", "11001" );
...
You can and should probably use string::replace.
Even with your most recent comment.
But this might have been what you were trying to do:
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <string>
using namespace std;
int main()
{
char temp;
map<char, char*> change;
string lol = "A APPLE AND BANANA";
change['A'] = "00011";
change['B'] = "11001";
change['C'] = "01110";
change[' '] = "$$";
for (int i = 0; i < lol.length(); i++)
{
temp = lol[i];
if (change[temp])
cout << change[temp];
else
cout << lol[i];
}
cout << endl;
cin.get();
return 0;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I tried to write lastindexOf function for my C++ class. After I was trying for 2 weeks, I still can't get it working.
At first , I was trying to follow the logic from this post: CString find the last entry , but since they use CString class instead of char class,I have no success to duplicate the code for char class. I also try the strstr, but I have no luck with that neither. I would appreciate any helps.
here is the code I have came up with so far :
#include
using namespace std;
int lastIndexOf(char *s, char target);
int main()
{
char input[50];
cin.getline(input, 50);
char h = h;
lastIndexOf(input, h);
return 0;
}
int lastIndexOf( char *s, char target)
{
int result = -1;
while (*s != '\0')
{
if (*s == target ){
return *s;
}}
return result;
}
Try this:
int lastIndexOf(const char * s, char target)
{
int ret = -1;
int curIdx = 0;
while(s[curIdx] != '\0')
{
if (s[curIdx] == target) ret = curIdx;
curIdx++;
}
return ret;
}