The Stack Overflow podcast is back! Listen to an interview with our new CEO.

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
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 ...
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: ...
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: ...
40
votes
5answers
23k 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 ...
39
votes
5answers
148k 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 ...
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,...
36
votes
5answers
3k views

Sorting strings by length - functional Python

I'm trying to port this little F# snippet while staying pythonic: ...
35
votes
8answers
8k 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 ...
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 ...
34
votes
9answers
84k 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 ...
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,...
28
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 Haskell. This ...
28
votes
2answers
102k 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-...
28
votes
1answer
65k 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 ...
27
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
5answers
10k 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 ...
27
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 ...
27
votes
4answers
7k 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 ...
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 ...
25
votes
10answers
71k views

Random string generation

I'm using this C# function to generate random coupons for a system. How can I improve it? ...
25
votes
2answers
31k 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? ...
24
votes
4answers
27k views

Reverse a String in Java

Here is the code for the CString class that I created. ...
24
votes
3answers
26k 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 ...
24
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, ...
24
votes
6answers
177k 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? ...
24
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 ...
24
votes
8answers
47k views

LINQ approach to flatten Dictionary to string

I have a Dictionary<string,string> and want to flatten it out with this pattern: ...
24
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 ...
23
votes
5answers
6k 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
9answers
32k 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: ...
23
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 ...
22
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. ...
21
votes
4answers
4k views

Count all vowels in string

For a homework assignemnt, 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
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 ...
21
votes
5answers
43k 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 ...
21
votes
5answers
2k 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)...
21
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 "blue is sky the". ...
21
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? ...
20
votes
7answers
24k views

Function for removing forbidden characters

...
20
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: ...
20
votes
7answers
29k 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" ...
20
votes
7answers
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: ...
19
votes
2answers
5k 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)\$. <...
19
votes
5answers
8k views

Check whether string ends with string

I wrote the following to check whether a string ends with a second string. ...
19
votes
2answers
1k views

Yet another shell in C

I wrote this shell in C about a semester ago for a university assignment on operating systems. Even though I got a score of 10/10 in this assignment, I doubt it deserved it - The pipeline ...
19
votes
4answers
22k 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 ...
19
votes
4answers
746 views

Name Filtering using a Trie

This question suggests a different (improved?) implementation for searching/filtering names based on a search query. This is based on the question here: Name filtering in Java A Trie allows the ...
18
votes
6answers
4k views

Explicit song lyrics checker

To stay in practice with my Python I've decided to write an explicit song checker. It checks each word in the song against an array of 14 explicit words contained in the text file. I've decided not to ...
18
votes
6answers
3k views

Testing whether characters of a string are unique

Critiques (even pedantic ones) are welcome. ...