Questions tagged [strings]
A string is a sequence of characters. It is commonly used to represent text or a sequence of bytes. Use this tag along with the appropriate programming language being used.
239
questions
41
votes
4answers
11k views
A lightning-fast StringBuilder
In the process of trying to build a serializable data structure, I found myself building large strings, which gets very slow because VBA copies a string every time concatenation is performed.
To ...
80
votes
4answers
21k views
Function to print command-line usage for a program [closed]
The following function works well to print out a help message for the program I have written. It prints a multi-line string message that describes the command line usage with some examples:
...
19
votes
3answers
2k views
A CSharpish String.Format formatting helper
A while ago I implemented .net's string.Format() method in VB6; it works amazingly well, but I'm sure there has to be a way to make it more efficient.
I'll start ...
22
votes
5answers
24k views
Split a string into chunks of the same length
Inspired by this question in chat
Is there an easy way in .NET to split a string into an array by pulling chunks of the text in. I.e. I have an 18 character string I would like in 3 6-character ...
42
votes
2answers
38k views
Balanced parentheses
Given an expression string exp, write a program to examine whether the
pairs and the orders of
"{","}","(",")","[","]"
are correct in exp.
For example,...
10
votes
1answer
4k views
Z-Algorithm for pattern matching in strings
I was trying to refactor the following Python code (keeping the same time-complexity) which is an implementation of Z-Algorithm for pattern matching in strings.
...
12
votes
4answers
3k views
Algorithm for Run Length Encoding - String Compression
I attempted a problem from the Cracking the Coding Interview book. The following input: aabcccaaa should give the following output: ...
6
votes
3answers
83k views
Calculate all possible combinations of given characters
I was asked in my textbook Lectures on Discrete Mathematics for Computer Science to construct a program that would take an alphabet ({a,b,c} or any combination of ...
8
votes
2answers
14k views
Simple string joiner in modern C++
(See the next iteration.)
I have this small template function for convenient dumping a sequence into a string such that there is a delimeter between two consecutive elements, and no such after the ...
6
votes
3answers
778 views
The Genetic Code
This question is part of a series solving the Rosalind challenges. For the previous question in this series, see Wascally wabbits. The repository with all my up-to-date solutions so far can be found ...
3
votes
2answers
83 views
Mixed Duo String Generator
I've create a program that finds mixed duo within a string. It removes characters from a string that do not form the first mixed duo. It does not rearrange the order of the string, it ony remove ...
15
votes
4answers
3k views
Int extension for translating integer to plain English
This code was inspired by this question: Number to Words
...
4
votes
1answer
2k views
Simplifying and optimizing factors and prime factorization generator
My first algorithm for finding the factors of a number was pretty slow and horrible (it started at \$O(n^2)\$, where n was not even the inputted number), but I eventually came up with this new code. ...
16
votes
5answers
188k views
Count number of each char in a String
I know there is a simpler way of doing this, but I just really can't think of it right now. Can you please help me out?
...
32
votes
7answers
65k views
Check for balanced parentheses
Given an expression string exp, write a program to examine whether the
pairs and the orders of
"{","}","(",")","[","]"
are correct in exp.
For example,...
42
votes
5answers
28k views
Determining if two words are anagrams
I was given a this problem at a technical interview and failed miserably in the time given. Afterwards, I sat down and worked out what I think is a good solution. The task was actually to build an ...
27
votes
7answers
7k views
Finding words of different lengths given six letters
I'm trying to find all the 3, 4, 5, and 6 letter words given 6 letters. I am finding them by comparing every combination of the 6 letters to an ArrayList of words ...
7
votes
6answers
16k views
Determining if a word is an anagram of another
The question was to see if a word is an anagram of another word.
How would you improve this? It returns True if an anagram, or False otherwise.
...
7
votes
3answers
2k views
Project Euler #8
I was doing Project Euler #8, which asks:
The four adjacent digits in the 1000-digit number that have the
greatest product are 9 × 9 × 8 × 9 = 5832.
...
6
votes
2answers
1k views
Named string interpolation
On machines where I don't have C# 6 I use this named string interpolation method. I tried to make it as pretty a possible as far as good coding practices are concerned but I just can't get rid of the ...
18
votes
6answers
3k views
12
votes
4answers
12k views
Align Strings for Output
I wrote a class which can format input so that it is properly aligned.
I'm interested in all suggestions (regarding structure, usability, style, naming, comments, etc).
I'm especially wondering ...
9
votes
3answers
467 views
n number of x on the y
Everyone knows "99 bottles of beer on the wall".
Mat's Mug made a comment about mugs on the wall in The 2nd Monitor and I realized I never wrote a "beer on the wall" program. But that seemed way too ...
8
votes
3answers
7k views
Converting a number to the text representation
Following along with some previous questions:
The @dbasnett original here: Number to Words
@nhgrif's here: Int extension for translating integer to plain English
I wanted to answer the original ...
7
votes
3answers
4k views
Fast alternative for String.split by whitespace
After profiling of one project I've found a bottleneck in the code, specifically the line:
String columns[] = line.split("\\s+");
I've tried to find a workaround,...
7
votes
1answer
706 views
A more readable InStr: StringContains
Consider the following:
If myString = "abc" Or myString = "def" [...] Or myString = "xyz" Then
In C# when myString == "abc" ...
5
votes
3answers
391 views
Karp-Rabin String Matching, Part 2
Part 1 is here. I wrote a Java implementation of the Karp-Rabin semi-numerical string matching algorithm. I incorporated the suggestions of @fge and @maaartinus from the other question into my new ...
5
votes
4answers
4k views
Find the shortest whole repetitive substring
I'm working on a problem to find wholly repeated shortest substring of a given string, and if no match, return length of the string.
My major idea is using a Trie ...
1
vote
2answers
5k views
replacing words with their abbreviations
I'm working on a program that aim to take sentences (currently in french) and compact them to a length of 38 characters while retaining as much information as possible.
You can find another part of ...
0
votes
2answers
2k views
JSON string parsing
I need to parse a simple JSON string (flat JSON, no hierarchy) for keys and values, and there is a system constraint that I cannot use any built-in JSON library and can only read a string once due to ...
12
votes
1answer
2k views
fgets() Alternative
size_t readline_tostring(char * restrict dest, size_t size, FILE * restrict stream)
fgets() is OK yet it has some short-...
10
votes
2answers
2k views
Title Case Converter
I wrote a program that, given a string of text, converts it to Title Case based on certain criteria. I would mainly like to know how I could improve readability and expressiveness, make a more clear ...
9
votes
1answer
5k views
Equation parser and solver
I wrote this equation parser and solver, I feel like it is well documented and tested:
...
9
votes
3answers
5k views
StringBuilder in C
Lots of languages have something that lets you build up a dynamically-sized string with minimal overhead. C doesn't, and I found myself using code that did that manually in a couple of places, so I ...
5
votes
5answers
20k views
Reverse complement of a DNA string
I have a function in python where I want the reverse complement of a DNA string.
This is what I have now:
...
5
votes
2answers
7k views
Print initial letter of an entered name
Just started learning python and I'm solving some beginner problems. I wrote a solution for a question without any edge cases like user not entering anything, entering numbers etc.:
Question: Write ...
4
votes
1answer
501 views
Improving time complexity of finding the longest palindrome in Python
The Longest Palindromic Substring challenge from InterviewBit:
Given a string S, find the longest palindromic substring in S.
where a "substring" must be contiguous, and in case of ties ...
2
votes
2answers
4k views
String 'expanding' - reinserting repeating characters
The challenge: Given a String, for each digit in that original string, replace that digit with that many occurrences of the character following it. So a3tx2z yields ...
1
vote
2answers
95 views
Neatly printing integers interspersed with spaces - follow-up
See the previous iteration.
At this point, I have
"digit group" length assumed to be always 3
the user can pass the delimiter character of his/her own
handles (seems to handle) the minus sign ...
239
votes
14answers
42k views
Calculating entropy of a string
We're calculating entropy of a string a few places in Stack Overflow as a signifier of low quality.
I whipped up this simple method which counts unique characters in a string, but it is quite ...
158
votes
14answers
58k views
Remove Last Comma
I want to join strings together, but when doing this it often happens that there is a comma too many, and therefore I need to remove that comma. In this code, I use the ...
40
votes
9answers
102k views
Converting between std::wstring and std::string
While researching ways to convert back and forth between std::wstring and std::string, I found this conversation on the MSDN ...
69
votes
1answer
5k views
NUMAH LITTERS OV KITTEHS ON TEH NETZ
It's not fair that 1% of the users hold 75% of the lolcode questions. #OccupyMatsMug
~ user2296177
I agree. Without further ado, here's the LOLCODE version of 99 bottles of beer on the wall:
...
26
votes
4answers
27k views
16
votes
2answers
15k views
Faster JavaScript fuzzy string matching function?
I'm using the following function to fuzzy match strings:
...
19
votes
6answers
37k views
Generate all possible combinations of letters in a word
This is a JavaScript function that returns ALL the possible combinations of whatever word the user enters. I am looking to clean this code up a bit...any and all suggestions are welcome!
...
18
votes
2answers
905 views
Enforcing string validity with the C# type system
One of my biggest complaints about .NET is that there's no way to guarantee a string match a certain type in the type-system.
Note
This started as more of a proof-of-concept than a real usable system,...
14
votes
2answers
14k views
C++ string_cast<> template function
In C++, to simplify string conversion between std::string and std::wstring, I created the following utility template functions:
...
11
votes
7answers
139k views
Reversing words in a string
I have to reverse the string "He is the one" to "one the is He". I have written some programs in Java but am looking for other best solutions. Suggest any possible ways to minimize the current ...
33
votes
2answers
108k views
Integer to Alphabet string (“A”, “B”, …“Z”, “AA”, “AB”…)
So this question is prompted by two things.
I found some code in our source control doing this sort of things.
These SO questions:
https://stackoverflow.com/questions/297213/translate-an-index-into-...