New answers tagged

0

You don't need to use Regex for this, and your regex doesn't cover most of cases anyway. You can simply splitting the input and try parse them into decimals. If they contains any invalid inputs then return three spaces (" ") otherwise print it in "00.000/00.000" format. Function StringFormatting(input As String) As String Dim parts = ...


0

The Go standard library strings package has a Join function: func Join(elems []string, sep string) string Your function is a simple extension of strings.Join: func JoinLast(elems []string, sep, lastSep string) string However, for no obvious reason, you chose a different function parameter signature: func JoinLast(sep string, lastSep string, words ...string)...


1

Unless case_insensitive_chrcmp() is supposed to be part of the public interface, declare it with static linkage. strpcmp is a name reserved for future Standard Library expansion. User programs should not declare identifiers beginning with str. This code makes assumptions about the language and the character coding: return ch1 == ch2 || ch1 + ' ' == ch2 ...


1

If username is not an email, getCensoredUsername(username) does nothing, which is a little bit confusing. Two possible approaches: Call the method censorLocalPart(String email) and use it only if the username is an email. (this suggestion is on the lines of @Fabio F.'s review) Add a comment (or Javadoc) to specify that a username will be censored only if it ...


7

There is nothing wrong about being "too Java 6"! First few notes about naming and naming convention: username is, in fact, the email, so should be email getCensoredUsername for the same reason should be getCensoredEmail but in Java get, by convention, is used only for getter or when you're applying very little logic; thus is better to use an "...


3

Just a few remarks.... if username doesn't contain a @ the code should return early which saves one level of indentation. The condition if (chars[i] == '.') should better be if (isCurrentCharacterADot) but you could omit this as well because you won't need to assign anything to chars[i] if it is a dot. This could look like so public static String ...


1

I don't think I understand the point of declaring a class if you aren't going to write any methods in it. I mean why go the expense of declaring a class just to store some variables outside of the global scope. Assuming this class can never populate its properties without parsing a document, so it make sense to have its constructor initiate the file reading ...


2

Your code does what you describe, but the code does not what it is really supposed to do. You are not the first person ever who needs to convert an arbitrary string into a form that can be embedded in a URL. Several people before you have already solved this problem. The crucial point is to know how these people named the method or function or class or ...


2

Review Welcome to Code Review. There are some suggestions as follows. Consider to use replaceAll The operation you performed is what String.replaceAll do, you can use replaceAll directly instead of String.toCharArray(), whitespace comparison, and sb.append things. Create a method URLify In order to improve usability, it is better to create a method URLify to ...


1

Note: I realize both that this is old and that there is a more direct answer. But I think that knowing how and when to use a StringBuilder is more important in general than solving this problem. public static String longToPlayerName(long name) { int i = 0; char[] nameCharacters = new char[12]; while (name != 0L) { long ll = name; ...


2

General Observations This code should work just fine, but performance can be enhanced. Initialize Arrays to Zero at Compile Time Currently there is a performance hit in the code when initializing the array t to zero. The function memset() is a runtime function, by initializing the array t to zero there is no run time cost: char t[32767] = {0}; The ...


1

In addition to the other answers: strpbrk or strcspn are useful for finding the next delimiter in a string. strspn can be used to find the next character that isn't a delimiter in a string. So given a char const* str as input, we could do something like: while (*str) { char const* end = strpbrk(str, delimiters); if (!end) end = strchr(str, '\0'); /*...


3

compare is a dubious name. The purpose of the function is to tell whether parse string contains a character. Consider bool contains(char * str, char ch); The special case does not check for a possible overflow. temp_matrix_length could reach temp_matrix_size by the time it is executed. I do not endorse a pointer notation here. string[i] is easier to read ...


5

Major remark of the code as whole: for some reason you write the unreadable version of array access *(arr+i) all over the place. Don't do that! This makes your code look needlessly obscure and hard to read. Instead use the much more readable arr[i]. Your compare is just a naive implementation of strchr. It would be much more efficient to use strchr. Don't ...


2

The problem is line *(words+j) = &str;. It says that we want words[j] or *(words+j) to point to where the str is pointing. So, at the end of the while, all the words are pointing to the same str with the content of red. It can be solved using strcpy method from string.h header, which copies the values not simply change the reference. strcpy(*(words + j), ...


1

Use consistent indentation I don't know if the indentation style of the code you pasted here is as you wrote it, but if it looks the same in your code editor, you should definitely try to fix it and make it more consistent. I would not bother trying to fix this manually, instead use either code formatting functions of your editor, or use an external tool ...


0

One approach that's simple, but is a bit of a dirty trick: first replace sequences of escape+separator with a string that's never going to occur in your text (for example a NUL byte "\x00"), then do the split, then do the reverse replace on each token. For example (Go Playground link): func SplitWithEscaping(s, separator, escape string) []string { ...


1

Most glaringly, [^0-9^A-Z^a-z^-^ ] reveals a lack of regex understanding. Your negated character class pattern breaks down as: [^ #match any character not listed... 0-9 #digits ^ #a caret symbol A-Z #uppercase letters ^ #a caret symbol a-z #lowercase letters ^-^ #all characters found in the ascii table between ^ and ^ ...


0

The only thing that I would perhaps comment on is that it could be done in a way that's shorter and simpler to understand (due to usage of LINQ and a different way of summing up calc (not done using the div by 2 formula)) if not every bit of performance wants to be squeezed out of the solution (I didn't handle chars directly, but worked with strings for ...


2

Two things I have noticed: You remove all non-alpha-numeric characters, apart from the dash and space characters. I don't see what this adds to your filter? Clearly all the other characters are never part of your swear words, so why bother to remove them? It could, in theory, have unpredictable consequences, because formerly separated characters might form ...


1

Apologies for changing the code that was answered. Since a few days ago I have evolved :) Yes, that function is ok and will work for any legal call and that macro will work only with two string literals, of course. The only change I want to add inside the function is to call strncmp() instead of reinventing it // two char arrays comparison int safer_strcmp_ ...


1

Firstly you might want to make use of a Haskell feature to combine both functions into one: unique :: Eq a => [a] -> [a] unique [] = [] unique xs = [x | (x,y) <- zip xs [0..], x `notElem` (take y xs)] Now let's talk about performance. Obviously lists aren't great in this respect, since take is quite expensive and needs to iterate through the list, ...


2

The problem with the representation (null-terminated char*, like C) is that it's inefficient for concatenation. If we use array+length, as is common for strings in many languages (including the C++ standard library), then we don't need to seek to the end for every operation (as we do here, hidden inside strcat() - even though we've already computed the ...


2

You did a single-null-allocation correctly(ish) once: if(str == nullptr){ this->str = new char; *(this->str) = '\0'; and incorrectly a second time: if(str == nullptr){ this->str = new char; this->str = '\0'; This will produce a memory leak. That aside, if your compiler allowed this without yelling about incompatible types, that ...


Top 50 recent answers are included