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
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
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
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
58 votes
5 answers
243k 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 ...
user avatar
  • 763
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
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
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
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
37 votes
8 answers
10k 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 ...
user avatar
37 votes
5 answers
4k views

Sorting strings by length - functional Python

I'm trying to port this little F# snippet while staying pythonic: ...
user avatar
34 votes
2 answers
114k 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-...
user avatar
  • 3,075
33 votes
1 answer
72k 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 ...
user avatar
  • 433
32 votes
10 answers
87k views

Random string generation

I'm using this C# function to generate random coupons for a system. How can I improve it? ...
user avatar
32 votes
3 answers
6k 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 ...
user avatar
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
32 votes
4 answers
13k 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 ...
user avatar
  • 21.7k
31 votes
4 answers
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 ...
user avatar
29 votes
5 answers
12k 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 ...
user avatar
  • 1,499
28 votes
6 answers
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, ...
user avatar
  • 383
28 votes
4 answers
60k 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? ...
user avatar
  • 924
28 votes
8 answers
58k views

LINQ approach to flatten Dictionary to string

I have a Dictionary<string,string> and want to flatten it out with this pattern: ...
user avatar
  • 383
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
27 votes
3 answers
31k 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 ...
user avatar
27 votes
2 answers
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? ...
user avatar
  • 273
26 votes
4 answers
28k views

Reverse a String in Java

Here is the code for the CString class that I created. ...
user avatar
26 votes
5 answers
99k 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 ...
user avatar
  • 415
26 votes
2 answers
133k 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 ...
user avatar
  • 947
26 votes
4 answers
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, ...
user avatar
  • 20.3k
26 votes
6 answers
210k 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? ...
user avatar
26 votes
4 answers
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 ...
user avatar
26 votes
2 answers
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 ...
user avatar
  • 8,569
25 votes
9 answers
41k 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: ...
user avatar
  • 359
25 votes
4 answers
6k 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. ...
user avatar
25 votes
3 answers
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 ...
user avatar
  • 5,057
25 votes
4 answers
93k views

Join List with Separator

I was implementing something similar to Python's join function, where join([a1, a2, ..., aN], separator :: String) returns ...
user avatar
  • 353
24 votes
7 answers
27k views

Function for removing forbidden characters

...
user avatar
  • 373
24 votes
5 answers
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)...
user avatar
24 votes
4 answers
36k 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 ...
user avatar
  • 58.1k
23 votes
5 answers
8k 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 ...
user avatar
  • 333
23 votes
5 answers
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? ...
user avatar
22 votes
9 answers
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 ...
user avatar
22 votes
6 answers
5k 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 ...
user avatar
22 votes
4 answers
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 ...
user avatar
  • 2,226
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
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
21 votes
5 answers
6k 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 ...
user avatar
  • 9,642
21 votes
4 answers
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: ...
user avatar
  • 1,473
21 votes
7 answers
32k 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" ...
user avatar
21 votes
6 answers
4k 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: ...
user avatar
  • 12.8k

1
2 3 4 5
58