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.
2,669
questions
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 ...
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:
...
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:
...
51
votes
5answers
212k views
String.IsNullOrEmpty in JavaScript
I am aware of it being frowned upon to do something like write C# in JavaScript. (see this if you don't know what I'm talking about)
But as a judgement call, I think we could stand to have a ...
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 ...
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,...
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 ...
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 ...
37
votes
8answers
9k views
C program that reverses a string
As practice with the (ever so painful) C Strings I wrote a string reversal program. I am new to C, so I wanted to make sure that I am not using bad coding practices. It runs just fine (at least with ...
37
votes
5answers
4k views
Sorting strings by length - functional Python
I'm trying to port this little F# snippet while staying pythonic:
...
33
votes
10answers
85k views
Random string generation
I'm using this C# function to generate random coupons for a system. How can I improve it?
...
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-...
32
votes
3answers
5k views
Message decoder
I've built a C decoder program. The length of an encoded message will be given and the message itself in the following line. All the characters of the message will be in uppercase letters. The task is ...
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,...
32
votes
4answers
10k views
Loading… animating dots in C
I've recently wanted to make a "Loading..." display in C where the dots print one at a time in order and then reset:
Suprisingly, there isn't much on the internet for doing this well, so I figured I ...
31
votes
1answer
71k views
Pack and unpack bytes to strings
I need to write a function that "packs" an array of bytes (integers between 0 and 255) into a string. I also need to be able to perform the reverse operation, to get my byte array from the string that ...
30
votes
4answers
5k views
I can has(kell) cheezburger?
Edit 13 June:
I have made a new and improved version here.
Original Question
This is a very rudimentary lolcats translator (it only works on the phrase "Can I have a cheeseburger") in ...
29
votes
5answers
11k views
Custom indexOf() without String methods
I created my own indexOf function. I was wondering if anyone could help me come up with a way to make it more efficient. I am practicing for interviews so the catch ...
28
votes
6answers
9k views
Matching a generated string of random letters to an input
I made a program in Python and wanted it to be faster, so I wrote it on C# because it's compiled. To my surprise, the Python program is much faster. I guess there is something wrong with my C# code, ...
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 ...
27
votes
2answers
32k views
Capitalize the first character of all words (even when following a '-')
I got it working, all variations are displayed Sint-Anna as should be, but I wonder, is there a simpler way to this, since it looks very cluttered?
...
26
votes
4answers
27k views
26
votes
3answers
29k views
Reusing StringBuilder or creating a new one? [closed]
Given that it is better to reuse objects than create new ones when developing with Android, is it worth while deleting the contents of a StringBuilder and reusing ...
26
votes
4answers
3k views
“ONCE”, “UPON”, “A”, “TIME”
I'm working on a small program to perform various tasks on text content, primarily at the word level. I wrote these methods as ways to help prepare the raw text file into something more malleable, ...
26
votes
6answers
198k views
Fast way of searching for a string in a text file
I have written this code to search for a string in a .txt file. Is it possible to optimize the code so that it searches for the string in fastest manner possible? ...
26
votes
4answers
2k views
Translating from English to Entean
I wrote a python script that converts English to Entean language from an anime called "The Devil is a part timer". I am sloppy at coding and know that the script can be made better.
In the anime, the ...
26
votes
2answers
1k views
Please Save My Name
I wanted to create a save game system for my city building game that did not require the player to input a name for the city. It was also important to allow infinite save games, so I could not just ...
25
votes
5answers
79k views
Replacing letters with numbers with its position in alphabet
If anything in the text isn't a letter, ignore it and don't return it.
a being 1, b ...
25
votes
2answers
123k views
Extract numbers from a string-Javascript
Consider the following string in javascript:
var string="border-radius:90px 20px 30px 40px";
I want to extract the 4 numbers from that string and store them in ...
25
votes
8answers
53k views
LINQ approach to flatten Dictionary to string
I have a Dictionary<string,string> and want to flatten it out with this pattern:
...
25
votes
3answers
4k views
What does the Bob say?
Description:
Bob is a lackadaisical teenager. In conversation, his responses are
very limited.
Bob answers 'Sure.' if you ask him a question.
He answers 'Whoa, chill out!' if you yell ...
24
votes
4answers
49k views
Get string truncated to max length
This is an extension method to get a string that is truncated to a maximum length.
Any comments?
...
24
votes
9answers
36k views
Most efficient way in C++ to strip strings
If I want to strip a string completely of its whitespaces, punctuation and numbers (i.e. anything that is not A-Z, a-z), what is the most efficient way of doing it in C++?
I tried this:
...
24
votes
4answers
5k views
Functions to convert camelCase strings to snake_case
I have written two functions, one actual and one helper, to convert camelCase strings into snake_case strings (I call it joint-lower case). Any ideas for improvement are welcome.
...
24
votes
5answers
2k views
Generating CSV strings for various 3rd party utilities
I'm generating CSV strings for various 3rd party utilities and this section of code gets repeated in many classes. Is there a better way to generate this string?
...
23
votes
5answers
7k views
Comma Code (project from “Automate the Boring Stuff with Python”)
I'm currently learning Python using the book mentioned in the title. I just finished reading about lists, tuples, functions and methods, and one of the projects following this chapter is to create a ...
23
votes
7answers
26k views
22
votes
9answers
3k views
Simple string reverse program in C++
I want to learn how to write good code. Starting off with simple programs for now. So any suggestions regarding style, readability or more efficient ways of implementation, etc. would really be ...
22
votes
5answers
3k views
Word Inverter in C
I've just made this program and I want to know how to improve it. It just receives a word and prints it backwards. Sequences of numbers are also accepted, but not white spaces (that's why I used scanf)...
22
votes
4answers
17k views
Reverse a string word by word
Given an input string, reverse the string word by word.
For example: Given s = "the sky is blue", return ...
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 ...
21
votes
5answers
5k views
Count all vowels in string
For a homework assignment, I was asked to complete this CodeStepByStep problem. It's coded correctly and gave me a right answer. I'm just wondering if there's an easier way to solve this problem ...
21
votes
4answers
18k views
Reverse words in a given String
I'd like this code to be improved
Input string: codereview is awesome
Output string: awesome is codereview
My approach:
...
21
votes
7answers
31k views
Check for balanced parentheses
I was given an assignment to check for balanced parentheses from an arbitrary string where parentheses are defined as (, [ or { and their respective "closing" ...
21
votes
6answers
3k views
Find the longest word's length
The challenge is simple:
Return the length of the longest word in the provided sentence.
The solution is just as simple:
...
21
votes
4answers
79k views
Join List with Separator
I was implementing something similar to Python's join function, where
join([a1, a2, ..., aN], separator :: String)
returns
...
20
votes
2answers
7k views
String reversal in Python
This code will take as output any string (long, empty, with spaces...) and will return its reverse. Can I improve this algorithm so that it can be faster?
Right now its complexity is \$O(n)\$.
<...
20
votes
4answers
2k views
Add 'ing' or 'ly' suffix to a string
I have been practising Python Practice questions and came across this question:
Write a Python program to add 'ing' at the end of a given string
(length should be at least 3).
If the given string ...
20
votes
4answers
70k views
Flattening a dictionary into a string
Given:
k = {'MASTER_HOST': '10.178.226.196', 'MASTER_PORT': 9999}
I want to flatten into the string:
...