Questions tagged [strings]

A "string" is a sequence of characters typically representing a unit of human-readable text. Questions on this topic deal with processing strings in programs, and how various languages and environments define and manipulate strings.

Filter by
Sorted by
Tagged with
3 votes
3 answers
6k views

Try to find a word in the dictionary that has most letters given

I recently came across an algorithm design problem that I'd like some help with: Given some letters, for example a,e,o,g,z,k,l,j,w,n and a dictionary of words. Find a word in the dictionary that has ...
  • 139
0 votes
1 answer
326 views

Algorithm Identification [ String & Dictionary ]

I recently heard of an interview question: Given a string and a dictionary. Break the string into meaningful words and I remember solving this before with dynamic programming fairly quickly (maybe ...
12 votes
5 answers
27k views

How do you compress ASCII strings into fewer bytes?

I'm working with an embedded device with a unique protocol that sends messages to other devices and I'm making an application that parses the sent packets. Each packet carries 8 bytes. The protocol is ...
  • 1,543
52 votes
6 answers
283k views

Is initializing a char[] with a string literal bad practice?

I was reading a thread titled "strlen vs sizeof" on CodeGuru, and one of the replies states that "it's anyways [sic] bad practice to initialie [sic] a char array with a string literal." Is this true,...
  • 1,443
33 votes
3 answers
45k views

Which string search algorithm is actually the fastest?

I have been stuck for some time on which is the fastest string search algorithm, heard many opinions, but in the end I'm not sure. I have heard some people saying that the fastest algorithm is Boyer-...
6 votes
1 answer
1k views

Finding occurrences of a useful words and phrases in strings

I am building an app that analyzes posts by people by pulling their Tweets and Facebook posts. I need to process all the posts and find useful phrases. What I mean by useful is that, any word or ...
3 votes
3 answers
669 views

Storing lots of large strings with frequent "appends" and few reads

In my current project, I need to store a very long ASCII string to each instance of a given object. This string will receive an 2 appends per minute and will not be retrieved so frequently. The ...
2 votes
2 answers
2k views

Looking for a non-cryptographic hash function that returns a single character

Suppose I have a dictionary of ASCII words stored in uppercase. I also want to save those words into separate files so that the total word count of each file is approximately the same. By simply ...
31 votes
2 answers
16k views

Why does Java use UTF-16 for internal string representation?

I would imagine the reason was fast, array like access to the character at index, but some characters won't fit into 16 bits, so it wouldn't work... So if you have to handle special cases anyways, ...
  • 2,623
1 vote
1 answer
761 views

How to implement string matching based on a pattern

I was asked to build a tool that can identify if a string match a pattern. Example: {1:20} stuff t(x) {a,b,c} would match: 1 stuff tx a 20 stuff t c It is a sort of regex but with a different ...
7 votes
2 answers
3k views

Complex string matching with fuzzywuzzy

I'm attempting to write a process that matches obscure strings to a single 'master string' for further processing. I have a lot of data that looks something like this: Basketball Basket Ball Football ...
  • 229
12 votes
1 answer
9k views

Shortest Common Superstring: find shortest string that contains all given string fragments

Given some string fragments, I would like to find the shortest possible single string ("output string") that contains all the fragments. Fragments can overlap each other in the output string. Example:...
  • 602
0 votes
1 answer
2k views

Removing occurrences of characters in a string

I am reading this book, Programming Interviews Exposed by John Mongan et. al and in chapter 6 they are discussing removing all instances of characters in a src string using a removal string, e.g. ...
14 votes
2 answers
39k views

When should I use StringBuilder or StringBuffer?

In a production web application, my fellow programmers used StringBuffer everywhere. Now I am taking care of application development and corrections. After reading StringBuilder and StringBuffer I ...
2 votes
2 answers
154 views

What direction should I consider the offset when searching strings in reverse?

I'm making an indexOfReverse utility function in my C++ program, and I have caught a bit of a snag. Implementing the offset and maxOffset in my indexOf was very intuitive to me.. These start from the ...
  • 123
2 votes
3 answers
473 views

Why are the arguments for substring functions mismatched?

In many languages, the substring function works like this: substring(startIndex, endIndex) returns the substring from startIndex until endIndex-1 (if you view startIndex and endIndex as 0-based) / ...
16 votes
7 answers
3k views

Should functions of a C library always expect a string's length?

I'm currently working on a library written in C. Many functions of this library expect a string as char* or const char* in their arguments. I started out with those functions always expecting the ...
18 votes
1 answer
6k views

How does PHP internally represent strings?

UTF8? UTF16? Do strings in PHP also keep track of the encoding used? Let's look at this script for example. Say I run: $original = "शक्नोम्यत्तुम्"; What actually happens? Obviously I think $...
  • 709
43 votes
1 answer
43k views

C++11 includes std::stoi, why not std::itos?

I noticed to my glee that C++11 has a std::sto@ family of functions for easily unpacking ints/floats/longs whatever from strings. I'm surprised however, that the opposite isn't implemented. Why didn't ...
  • 11.6k
11 votes
6 answers
5k views

Is it inefficient to concatenate strings one at a time?

I recall back from my days of programming in C that when two strings are joined, the OS must allocate memory for the joined string, then the program can copy all the string text over to the new area ...
  • 591
30 votes
6 answers
30k views

Should a string constant be defined if it's only going to be used once?

We're implementing an adapter for Jaxen (an XPath library for Java) that allows us to use XPath to access the data model of our application. This is done by implementing classes which map strings (...
  • 520
1 vote
4 answers
188 views

Possible applications of algorithm devised for differentiating between structured vs random text

I have written a program that can rapidly (within 5 sec on a 2GB RAM desktop, 2.33 Ghz CPU) differentiate between structured text (e.g English text) and random alphanumeric strings. It can also ...
  • 11
9 votes
2 answers
2k views

Why are Python strings allocated on the stack?

According to this Python code visualizer, Python strings are allocated on the stack and not on the heap. Why is this? I thought they would be similar to Java where Strings are allocated on the heap.
  • 3,253
24 votes
4 answers
42k views

How can I extract words from a sentence and determine what part of speech each is? [closed]

I want to write something that takes a sentence and identifies each word it contains and defines what part of speech each word is. For example Hello World, I am a sentence would return this ...
  • 259
9 votes
7 answers
51k views

string.format with variables vs inline variables

What are the pros/cons (if any) to using string output; int i = 10; output = string.Format("the int is {0}", i); versus string output; int i = 10; output = "the int is " + i; I have always used ...
  • 641
7 votes
5 answers
7k views

Alternate string formatting options in C++?

I'm looking at optimizing some string formatting code that's hit a lot in our code. We had been using ostringstream, and I converted the code to use sprintf (actually Microsoft's more secure sprintf_s)...
  • 11.6k
15 votes
3 answers
11k views

What performance can we expect from std::string's c_str()? Always constant time?

I've been doing some needed optimizations lately. One thing I've been doing is changing some ostringstreams -> sprintfs. I'm sprintf'ing a bunch of std::strings to a c style array, ala char foo[500]; ...
  • 11.6k
4 votes
3 answers
16k views

Best practice Java - String array constant and indexing it

For string constants its usual to use a class with final String values. But whats the best practice for storing string array. I want to store different categories in a constant array and everytime a ...
  • 305
11 votes
2 answers
507 views

How to select a most probable option from the list based on user text Input

I am working over a OCR Application where I need to select a option based on user text input. Ex: Available Options: ["python", "ruby", "java", "clojure", "haskell"] Input: kava Output: java Input: ...
16 votes
3 answers
2k views

Is it possible to write a generalized string reverse function that works for all localisations and string types?

I was just watching the Jon Skeet (with Tony the Pony) presentation from Dev-Days. Although "write a string reverse function" is coding interview 101 - I'm not sure that it's actually possible to ...
9 votes
6 answers
2k views

What are the pros and cons of having a CaseInsensitiveString type in Java? [closed]

I'm tempted to create a final class CaseInsensitiveString implements CharSequence. This would allow us to define variables and fields of this type, instead of using a regular String. We can also have ...
9 votes
5 answers
9k views

Why do people put '\n' at the beginning of strings?

Very often I get into C code where printf format strings start with \n: printf( "\nHello" ); This in my opinion is an annoying thing that offers no advantages (rather many disadvantages!) with ...
  • 263
1 vote
3 answers
4k views

Search for a sub-string in a given array of strings

I have an array of n strings. I want to select all the elements of the array that starts with the given string. Sorry if that is not clear. I'll give an example. input = "as" array = ["abas", "aras",...
12 votes
4 answers
1k views

How defensive should we be?

We've been running Pex over some code, and it has been showing some good things (well bad things, but showing them before it gets to production!). However, one of the nice things about Pex is that it ...
  • 3,816
4 votes
3 answers
733 views

What should I consider when converting from ASCII to Unicode?

For instance if I had only ASCII characters and then switched to Unicode now I have to consider special characters, and bigger strings or chars. What else along these lines should I be considering? I'...
9 votes
3 answers
12k views

Why does Java's String class not implement a more efficient indexOf()?

Following the below question on Stack Overflow https://stackoverflow.com/questions/5564610/fast-alernative-for-stringindexofstring-str I got to wondering why it is that java (6 at least) not use a ...
  • 219
3 votes
1 answer
735 views

Types of quotes for an HTML templating language

I'm developing a templating language, and now I'm trying to decide on what I should do with quotes. I'm thinking about having 3 different types of quotes which are all handled differently: ...
  • 1,889
7 votes
11 answers
701 views

What should I call the process of converting an object to a string?

We are having a game of 'semantic football' in the office over this matter: I am writing a method for an object which will represent the object as a string. That string should be such that when typed (...
63 votes
3 answers
5k views

Etymology of "String"

So it's obvious that a string of things is a sequence of things, and so a sequence of characters/bytes/etc. might as well be called a string. But who first called them strings? And when? And in what ...
  • 1,000
81 votes
12 answers
145k views

SQL: empty string vs NULL value

I know this subject is a bit controversial and there are a lot of various articles/opinions floating around the internet. Unfortunatelly, most of them assume the person doesn't know what the ...
  • 2,254
16 votes
16 answers
6k views

What do you use string reversal for? [closed]

In PHP it's strrev(), in Rails it's .reverse, but most languages don't have any string reverse function. Some have array reverse functions that can be used on characters. I was thinking this must be a ...
6 votes
2 answers
610 views

Why do people confuse C-type strings and STL strings [closed]

The last week I saw at least three questions where people were confused about the differences between char * and std::string or made some related mistakes. Don't get me wrong, I don't have a problem ...
  • 565
10 votes
1 answer
794 views

String class based on graphemes?

I'm wondering why we don't have some string classes that represent a string of Unicode grapheme clusters instead of code points or characters. It seems to me that in most applications it would be ...
  • 101
23 votes
5 answers
4k views

Why are strings so slow?

Ever since my very first programming class in high school, I've been hearing that string operations are slower — i.e. more costly — than the mythical "average operation." Why makes them so ...
  • 4,105

1 2 3
4