decoding a character array [closed] - c++

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 7 years ago.
Improve this question
Given a character array in a URL format like
char *s="www.google.com\tsp finite.aspx"
While decoding it space should be replaced by %20 and thus the string becomes:
char *s="www.google.com\tsp%20finite.aspx"
We are not allowed to use a new character array but allowed to use some character variables for temporary use. Should not use any containers also. The array contains enough space to contain the decoded data so no need to worry about the more space to be taken.
I followed the Brute-Force mechanism where all the characters from the point of finding a space to the end of the array are to be swapped. But this is not the most efficient way to solve the problem.
Can any body tell me which is the best way (algorithm) to decrease the no. of swappings in order to acquire the solution.

I am assuming the string has been allocated using malloc
First calculate the number of spaces and the length of the string
Then the new length = old length + number of spaces * 2. Use realloc to expand the string.
The work backwards from the end and copy to the new length. When encountering space copy in %20 instead.

The main problem could be that swapping space with %20 will require moving the whole string 2 characters more .
Here's an idea :
Parse the whole string once, and count the number of spaces in the string
The new length of the array would be strlen(original) + 2*(nOfSpaces) (let's call it from now on NewLen)
Parse the whole string once again but starting backwards.
You will copy the previous string contents inside itself but at an offset until you hit a space
you will have a pointer starting at strlen(original) and one starting at NewLen
parse from strlen(original) backwards until you find a space (the substrLen will be subLen)
memcpy from [strlen(original)-curParsingindex] to [NewLen - curParsingIndex-2*(enteredSpaces)] sublen amount
Instead of copying the space, put %20 instead
This way you will avoid moving the string forward each time you hit a space.
Regarding step 4 , you might think about using a temporary variable for the sublen, since you might end up writing in the same memory zone by mistake (take an example where all the spaces are at the beginning).

This is a classic interview coding question; a good solution for this starts with a good interface for your solution. Something that works is:
char* replaceChar(char* in, char c)
char *in - string you want to decode
c - the char you want to replace with it's hexa value ASCII code ( HEX val ascii for ' ' is 0x20)
Pseudocode:
allocate a buffer the same size as the input buffer
get the index of the first occurrence of the char you want to replace (strcspn can help with that)
copy the content of the of the input up to the found index to the new buffer.
reallocate the new buffer size to newSize=oldSize+2
add % to the new string
repeat until you reach the end of the string.
return a pointer to the new string
You can also do it in place on the original string but that solution is a bit more complicated because you have to shift everything.

You can do it in two passes. The key idea is to first count the number of spaces and then move each character directly to its final position. In your approach you shift the remainder of the string at each occurrence of a space.
#include <stdio.h>
int main ()
{
char str[1000] = "www.example.com/hello world !";
int length;
int spaces;
int i;
char *ptr;
printf ("\"%s\"\n", str);
// pass 1:
// calculate length and count spaces
length = 0;
spaces = 0;
for (ptr = str; *ptr; ptr++) {
if (*ptr == ' ') {
spaces++;
}
length++;
}
// pass 2:
// transform string
// preserve terminating null character
ptr = str + length + 2 * spaces;
for (i = length; i >= 0; i--) {
char c = str[i];
if (c == ' ') {
*ptr-- = '0';
*ptr-- = '2';
*ptr-- = '%';
}
else {
*ptr-- = c;
}
}
printf ("\"%s\"\n", str);
return 0;
}

Related

Having some confusion with pointers [closed]

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 11 months ago.
Improve this question
I have the following code that I'm trying to decipher from a former colleague:
void getIP(int p){
char tstr[80];
char *pt;
strcpy(ipBuf,"NA");
if(p==-1)return;
strcpy(tstr, panes[p].stream.c_str());
pt=strchr(tstr,':');
if(pt){
*pt='x'; //Blank first one
pt=strchr(tstr,':');
if(pt){
*pt='\0';
strcpy(ipBuf,&tstr[7]);
}
}
}
I'm relatively inexperienced with C++ so was hoping I could get some help with how this code works. Its purpose I think is to take a camera stream address and strip the port number and any extra stuff off to just give an IP address. I cannot understand through how it achieves this other than it seems to use ":" as a delimiter at a couple of stages?
To explain the function a little more, int p is a position on the grid, then it takes the stream address from that grid square and puts it into tstr.
But any explanation beyond that is much appreciated.
I [...] was hoping I could get some help with how this code works.
strcpy(tstr, panes[p].stream.c_str());
Copy the contents of the std::string designated by panes[p].stream into array tstr, yielding an independent copy as a C string.
pt=strchr(tstr,':');
if(pt){
*pt='x'; //Blank first one
Locate the first appearance of a ':' character in the local copy of the string. If the character is found, then replace it with an 'x'.
pt=strchr(tstr,':');
if(pt){
*pt='\0';
Locate the (new) first appearance of a ':' character in the local copy of the string. If it exists, replace it with a '\0', which will be recognized as a string terminator. That is, truncate the string at that point.
strcpy(ipBuf,&tstr[7]);
Copy the contents of the local C string, starting at the eighth character (because arrays are indexed from 0, not from 1), into the space to which ipBuf points. The magic number 7 is suspicious here, but I don't have enough information to be able to determine whether it is erroneous. My guess would be that the code is assuming that the original first colon will always appear at index 6, with the result that the substring between the (original) first and second colons is copied, but there are cleaner, clearer, more efficient ways to do that.
Improved version, with more C++
... and better names, and no unnecessary copying, and explanatory comments:
void getIP(int pane_index){
if (pane_index >= 0) {
// for clarity and convenience
std::string &url_string = panes[pane_index].stream;
// hardcoded per the original code
const size_t ip_offset = 7;
// Locate the first colon, if any, after the start of the ip address
size_t colon_index = url_string.find(':', ip_offset);
if (colon_index != std::string::npos) {
// Extract the name / IP address as the substring starting at
// offset 7 and stopping just before the second colon
size_t ip_len = colon_index - ip_offset;
strncpy(ipBuf, url_string.c_str() + ip_offset, ip_len);
ipBuf[ip_len] = '\0';
return;
} // else there are no colons after the start of the ip address
} // else an invalid pane index was given
// no machine name / IP address is available
strcpy(ipBuf, "NA");
}
As already noted in the previous comment, the function searches for the second occurence of ':' in tstr string and if it is found, replaces it with null character, effectively cutting off any remaining characters from the string. Than the characters in the string from index [7] to the null character (previously ':') are copied to ipBuf. For example, let say url is rtsp://192.168.0.200:551/stream.sdp. Digit 1 in "192" has index [7] in the string. So copying from that position to the second ":" (or null char) would copy "192.168.0.200".

Array conversion guidance

I'm stuck on an assignment which converts contents of an array (input from the user) to a pre-declared shorthand.
I want it to be as simple as strcpy(" and ", "+");
to change the word 'and' within a string, to a '+' sign.
Unfortunately, no matter how I structure the function; I get a deprecated conversion warning (variant loops, and direct applications, attempted).
Side note; this is assignment based, so my string shortcuts are severely limited, and no pointers (I've seen several versions of clearing the fault using them).
I'm not looking for someone to do my homework; just guidance on how strcpy can be applied without creating the dep. warning. Perhaps I shouldn't be using strcpy at all?
strcpy copies the contents of the second string into the memory of the first string. Since you're copying a string literal into a string literal it can't do it (you can't write to a string literal) and so it complains.
Instead you need to build your own search and replace system. You can use strstr() to search for a substring within a string, and it returns the pointer in memory to the start of that found string (if it's found).
Let's take the sample string Jack and Jill went up the hill.
char *andloc = strstr(buffer, " and ");
That would return the address of the start of the string (say 0x100) plus the offset of the word " and " (including spaces) within it (0x100 + 4) which would be 0x104.
Then, if found, you can replace it with the & symbol. However you can't use strcpy for that as it'll terminate the string. Instead you can set the bytes manually, or use memcpy:
if (andloc != NULL) { // it's been found
andloc[1] = '&';
andloc[2] = ' ';
}
or:
if (andloc != NULL) { // it's been found
memcpy(andloc, " & ", 3);
}
That would result in Jack & d Jill went up the hill. We're not quite there yet. Next you have to shuffle everything down to cover the "d " from the old " and ". For that you'd think you could now use strcpy or memcpy, however that's not possible - the strings (source and destination) overlap, and the manual pages for both specifically state that the strings must not overlap and to use memmove instead.
So you can move the contents of the string after the "d " to after the "& " instead:
memmove(andloc + 3, andloc + 5, strlen(andloc + 5) + 1);
Adding a number to a string like that adds to the address of the pointer. So we're looking at copying the data from 5 characters further on in the string that the old "and" location into a space starting at 3 characters on from the start of the old "and" location. The amount to copy is the length of the string from 5 characters on from the start of the "and" location plus one so it copies the NULL character at the end of the string.
Another manual way of doing it would be to iterate through each character until you find the end of the string:
char *to = andloc + 3;
char *from = andloc + 5;
while (*from) { // Until the end of the string
*to = *from; // Copy one character
to++; // Move to the ...
from++; // ... next character pair
}
*to = 0; // Add the end of string marker.
So now either way the string memory contains:
Jack & Jill went up the hill\0l\0
The \0 is the end of string marker, so the actual string "content" is only up as far as the first \0 and the l\0 is now ignored.
Note that this only works if you are replacing a part with something that is smaller. If you are replacing it with something bigger, so the string grows in size, you will be forced to use memmove, which first copies the content to a scratchpad, and ensure that your buffer has enough room in it to store the finished string (this kind of thing is often a big source of "buffer overruns" which are a security headache and one of the biggest causes of systems being hacked). Also you have to do the whole thing backwards - move the latter part of the string first to make room, then modify the gap between the two halves.

remove non alphabet characters from string c++ [duplicate]

This question already has answers here:
How to strip all non alphanumeric characters from a string in c++?
(12 answers)
Closed 6 years ago.
I'm trying to remove all non alphabet characters from an inputed string in c++ and don't know how to. I know it probably involves ascii numbers because that's what we're learning about. I can't figure out how to remove them. We only learned up to loops and haven't started arrays yet. Not sure what to do.
If the string is Hello 1234 World&*
It would print HelloWorld
If you use std::string and STL, you can:
string s("Hello 1234 World&*");
s.erase(remove_if(s.begin(), s.end(), [](char c) { return !isalpha(c); } ), s.end());
http://ideone.com/OIsJmb
Note: If you want to be able to handle strings holding text in just about any language except English, or where programs use a locale other than the default, you can use isalpha(std::locale).
PS: If you use a c-style string such as char *, you can convert it to std::string by its constructor, and convert back by its member function c_str().
If you're working with C-style strings (e.g. char* str = "foobar") then you can't "remove" characters from a string trivially (as a string is just a sequence of characters stored sequentially in memory - removing a character means copying bytes forward to fill the empty space used by the deleted character.
You'd have to allocate space for a new string and copy characters into it as-needed. The problem is, you have to allocate memory before you fill it, so you'd over-allocate memory unless you do an initial pass to get a count of the number of characters remaining in the string.
Like so:
void BlatentlyObviousHomeworkExercise() {
char* str = "someString";
size_t strLength = ... // how `strLength` is set depends on how `str` gets its value, if it's a literal then using the `sizeof` operator is fine, otherwise use `strlen` (assuming it's a null-terminated string).
size_t finalLength = 0;
for(size_t i = 0; i < strLength; i++ ) {
char c = str[i]; // get the ith element of the `str` array.
if( IsAlphabetical(c) ) finalLength++;
}
char* filteredString = new char[ finalLength + 1 ]; // note I use `new[]` instead of `malloc` as this is C++, not C. Use the right idioms :) The +1 is for the null-terminator.
size_t filteredStringI = 0;
for(size_t i = 0; i < strLength; i++ ) {
char c = str[i];
if( IsAlphabetical(c) ) filteredString[ filteredStringI++ ] = c;
}
filteredString[ filteredStringI ] = '\0'; // set the null terminator
}
bool IsAlphabet(char c) { // `IsAlphabet` rather than `IsNonAlphabet` to avoid negatives in function names/behaviors for simplicity
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}
I do not want to spoil the solution so I will not type out the code, only describe the solution. For your problem think of iterating through your string. Start with that. Then you need to decide if the currently selected character is part of the alphabet or not. You can do this numerous different ways. Checking ASCII values? Comparing against a string of the alphabet? Once you decide if it is a letter, then you need to rebuild the new string with that letter plus the valid letters before and after that you found or will find. Finally you need to display your new string.
If you look at an ascii table, you can see that A-Z is between 65-90 and a-z is between 97-122.
So, assuming that you only need to remove those characters (not accentuated), and not other characters from other languages for example, not represented in ascii, all you would need to do is loop the string, verify if each char is in these values and remove it.

Efficiently appending or inserting a variable number of spaces to a string

I have a simple programme that inserts or appends a number of spaces to align text.
f()
{
string word = “This word”;
const string space = “ “;
int space_num = 5; // this number can vary
for (int i = 0; i < space_num; i++)
{
word.insert(0, space);
}
cout << word;
}
Now this works, but I was wondering if there was a more efficient way to do this. Not in terms of optimizing my programme, but more as in standard practice.
I can imagine two potential methods:
1 - Is there a way to create a string of say 20 spaces, and append a portion of those spaces rather than repeatedly adding a single space.
2 – Is there a way to create string with a variable number of spaces and append that?
Yes, both take a number of copies and a character:
word.insert(0, space_num, ' ');
word.append(space_num, ' ');
For aligning text, keep in mind you can use a string stream and the <iomanip> header, such as std::setw as well.
1 - Is there a way to create a string of say 20 spaces, and append a portion of those spaces rather than repeatedly adding a single space.
Yes, try this:
string spaces(20, ' ');
string portionOfSpaces = spaces.substr(0,10); //first 10 spaces
string newString = portionOfSpaces + word;
Generally, you can use substr to get a portion of spaces and do operations with that substring.
2 – Is there a way to create string with a variable number of spaces and append that?
Yes, see string constructor:string (size_t n, char c); and string::append

How to extract data from a char[] string

Currently I have a GPS connected to my Arduino chip which outputs a few lines every second. I want to extract specific info from certain lines.
$ÇÐÇÇÁ,175341.458,3355.7870,Ó,01852.4251,Å,1,03,5.5,-32.8,Í,32.8,Í,,0000*57
(Take note of the characters)
If I read this line into a char[], is it possible to extract 3355.7870 and 01852.4251 from it? (Well obviously it is, but how?)
Would I need to count the commas and then after comma 2 start putting the number together and stop at comma 3 and do the same for second number or is there another way? A way to split up the array?
The other problem with this is identifying this line because of the strange characters at it's beginning - how do I check them, because their not normal and behaves strangely?
The data I want is always in form xxxx.xxxx and yyyyy.yyyy and are unique in that form, meaning I could maybe search trough all the data not caring about which line it's on and extract that data. Almost like a preg-match, but I have no idea how to do that with a char[].
Any tips or ideas?
You can tokenize (split) the string on the comma using strtok, and then parse the numbers using sscanf.
Edit: C example:
void main() {
char * input = "$ÇÐÇÇÁ,175341.458,3355.7870,Ó,01852.4251,Å,1,03,5.5,-32.8,Í,32.8,Í,,0000*57";
char * garbage = strtok(input, ",");
char * firstNumber = strtok(NULL, ",");
char * secondNumber = strtok(NULL, ",");
double firstDouble;
sscanf(firstNumber, "%lf", &firstDouble);
printf("%f\n", firstDouble);
}
If you have strange characters at the beginning of the string, then you should start parsing it from the end:
char* input = get_input_from_gps();
// lets assume you dont need any error checking
int comma_pos = input.strrchr(',');
char* token_to_the_right = input + comma_pos;
input[comma_pos] = '\0';
// next strrchr will check from the end of the part to the left of extracted token
// next token will be delimited by \0, so you can safely run sscanf on it
// to extract actual number