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
42 votes
4 answers
12k 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 ...
user avatar
  • 1,114
81 votes
4 answers
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: ...
user avatar
  • 971
19 votes
3 answers
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 ...
user avatar
22 votes
5 answers
26k 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 ...
user avatar
  • 50k
43 votes
2 answers
39k 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,...
user avatar
  • 32.7k
11 votes
1 answer
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. ...
user avatar
  • 221
13 votes
4 answers
4k 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: ...
user avatar
  • 951
6 votes
3 answers
87k 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 ...
user avatar
9 votes
3 answers
19k views

Simple string joiner in modern C++

(See the next iteration.) I have this small template function for conveniently dumping a sequence into a string such that there is a delimiter between two consecutive elements, and no such after the ...
user avatar
  • 26k
12 votes
1 answer
3k views

fgets() Alternative

size_t readline_tostring(char * restrict dest, size_t size, FILE * restrict stream) fgets() is OK yet it has some short-...
user avatar
6 votes
3 answers
870 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 ...
user avatar
  • 12.8k
3 votes
2 answers
95 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 ...
user avatar
  • 99
15 votes
4 answers
3k views

Int extension for translating integer to plain English

This code was inspired by this question: Number to Words ...
user avatar
  • 25.2k
4 votes
1 answer
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. ...
user avatar
  • 34.7k
17 votes
5 answers
189k 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? ...
user avatar
  • 173
32 votes
7 answers
66k 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,...
user avatar
43 votes
5 answers
30k 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 ...
user avatar
  • 539
27 votes
7 answers
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 ...
user avatar
  • 481
7 votes
6 answers
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. ...
user avatar
  • 257
7 votes
3 answers
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. ...
user avatar
6 votes
2 answers
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 ...
user avatar
  • 43.8k
19 votes
6 answers
3k views

Testing whether characters of a string are unique

Critiques (even pedantic ones) are welcome. ...
user avatar
  • 193
12 votes
4 answers
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 ...
user avatar
  • 24.9k
10 votes
2 answers
841 views

A more readable InStr: StringContains

Consider the following: If myString = "abc" Or myString = "def" [...] Or myString = "xyz" Then In C# when myString == "abc" ...
user avatar
9 votes
3 answers
483 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 ...
user avatar
  • 12.8k
8 votes
3 answers
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 ...
user avatar
  • 96.8k
7 votes
3 answers
5k 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,...
user avatar
  • 211
5 votes
3 answers
412 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 ...
user avatar
  • 970
5 votes
4 answers
6k 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 ...
user avatar
  • 3,443
1 vote
2 answers
8k 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 ...
user avatar
0 votes
2 answers
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 ...
user avatar
  • 3,443
10 votes
2 answers
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 ...
user avatar
10 votes
3 answers
7k 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 ...
user avatar
  • 5,463
9 votes
1 answer
5k views

Equation parser and solver

I wrote this equation parser and solver, I feel like it is well documented and tested: ...
user avatar
  • 27.3k
7 votes
5 answers
11k views

Creating a numeric phone keypad

I'm writing a Telephone class and it has a method called getDigits which takes in a String parameter and returns the number that would appear on your phone if you ...
user avatar
6 votes
5 answers
31k 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: ...
user avatar
  • 687
5 votes
2 answers
8k 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 ...
user avatar
4 votes
1 answer
554 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 ...
user avatar
3 votes
2 answers
266 views

str_join() function, not present in the standard C library

This function str_join() is an updated version of my earlier function strcat_new() — found here: ...
user avatar
3 votes
2 answers
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 ...
user avatar
  • 9,779
1 vote
2 answers
112 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 ...
user avatar
  • 26k
246 votes
14 answers
47k 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 ...
user avatar
  • 3,515
166 votes
15 answers
64k 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 ...
user avatar
  • 1,787
40 votes
9 answers
114k 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 ...
user avatar
74 votes
1 answer
6k 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: ...
user avatar
  • 12.8k
26 votes
4 answers
28k views

Reverse a String in Java

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

Faster JavaScript fuzzy string matching function?

I'm using the following function to fuzzy match strings: ...
user avatar
  • 1,101
21 votes
6 answers
8k views

I created something much faster than a std::string

I've recreated std::string in a way that might be faster. System Features: Much faster than std::string. Easy to use. It was designed as part of a large database ...
user avatar
19 votes
6 answers
44k 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! ...
user avatar
18 votes
2 answers
985 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,...
user avatar

1
2 3 4 5 6