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.

Filter by
Sorted by
Tagged with
34
votes
4answers
8k 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 ...
75
votes
4answers
20k 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: ...
17
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 ...
17
votes
4answers
19k 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 ...
9
votes
1answer
3k 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. ...
39
votes
2answers
35k 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,...
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
76k 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 ...
7
votes
2answers
9k 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 ...
3
votes
2answers
82 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
2k 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
187k 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? ...
29
votes
7answers
64k 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,...
40
votes
5answers
22k 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 ...
26
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
1k 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
851 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

Testing whether characters of a string are unique

Critiques (even pedantic ones) are welcome. ...
11
votes
4answers
11k 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
462 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
6k 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 ...
6
votes
3answers
724 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 ...
6
votes
3answers
3k 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,...
5
votes
3answers
371 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
2k 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
2k 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 ...
10
votes
2answers
1k 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 ...
8
votes
3answers
4k 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 ...
8
votes
1answer
4k views

Equation parser and solver

I wrote this equation parser and solver, I feel like it is well documented and tested: ...
6
votes
1answer
599 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
5answers
12k 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
5k 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
407 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 the first ...
2
votes
2answers
3k 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
91 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 ...
227
votes
14answers
37k 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 ...
149
votes
14answers
47k 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 ...
34
votes
9answers
83k 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 ...
24
votes
4answers
27k views

Reverse a String in Java

Here is the code for the CString class that I created. ...
16
votes
2answers
13k views

Faster JavaScript fuzzy string matching function?

I'm using the following function to fuzzy match strings: ...
62
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: ...
17
votes
2answers
854 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 ...
17
votes
6answers
30k 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! ...
13
votes
2answers
12k 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
138k 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 ...
28
votes
2answers
101k 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-...
19
votes
4answers
21k views

Create a C++ string using printf-style formatting

It's often convenient to use C-style printf format strings when writing C++. I often find the modifiers much simpler to use than C++ I/O manipulators, and if I'm ...