Skip to content

Instantly share code, notes, and snippets.

@cbourke
Created October 9, 2018 17:11
Show Gist options
  • Select an option

  • Save cbourke/39b741d4a0b601f1156f36bf0ff16ee8 to your computer and use it in GitHub Desktop.

Select an option

Save cbourke/39b741d4a0b601f1156f36bf0ff16ee8 to your computer and use it in GitHub Desktop.
auto-posted gist

CSCE 155H - Computer Science I Honors

Fall 2018

Strings

  • Strings are ordered sequences of characters (may be ASCII or Unicode)
  • Different languages represent strings differently
  • Most languages provide a standard library of functions/methods to process strings

Strings in C

  • In C, strings are arrays of char elements
  • BUT: in C, all strings must end with a special null-terminating character, \0, NOT the same thing as NULL nor 0 nor void
  • You can declare/use static strings:
char message[] = "hello World!"; //a size 13 character array

//strings in C are mutable:
message[0] = 'H';
  • In the above message is automatically null-terminated and has 12 valid characters, but is of size 13 (to accommodate the null-terminator)
  • You can treat strings in C like arrays because they are!
message[5] = '\0';

printf("message = %s\n", message); //prints "Hello"

message[5] = '_';

printf("message = %s\n", message); //prints "Hello_World!"

message[0] = '\0'; //now the "empty string"
  • What happens when a string is not properly null terminated?
  • You can get garbage results, segmentation faults, "undefined behavior"

Dynamic strings

  • You can use malloc to allocate enough space to hold whatever strings you want to represent
int n = 100;
char *name = (char *) malloc( (n+1) * sizeof(char));

name[0] = 'C';
name[1] = 'h';
name[2] = 'r';

//you CANNOT use:
name = "Christopher Bourke";
//memory leak! don't do it

String Library

  • There is a standard string library in string.h
  • Dozens of useful functions

Assignment/Copy Function

  • strcpy: takes two arguments, a "destination" and a "source"
int n = 100;
char *name = (char *) malloc( (n+1) * sizeof(char));

strcpy(name, "Christopher Bourke");
//now name contains "Christopher Bourke\0"
  • strcpy will copy the null-terminating character for us!
  • However, strcpy assumes that the destination is big enough to hold what you are trying to copy

String Length

  • strlen: it takes a string and returns an integer
  • It returns the number of valid character in the string (it does NOT include the null terminating character)
char message[] = "Hello World!";
int n = strlen(message); //12

Concatenation

  • strcat: takes two arguments, the destination and the source
  • It appends or "concatenates" the source to the END of the destination string
int n = 100;
char *fullName = (char *) malloc( (n+1) * sizeof(char));
lastName = "Bourke";
firstName = "Chris";

strcpy(fullName, lastName);
strcat(fullName, ", ");
strcat(fullName, firstName);
//the content of fullName is "Bourke, Chris"
  • strcat takes care of the null-terminator for us
  • Like strcpy, it assumes that the destination string is big enough to hold the entire

Length-limited Versions

  • strncpy and strncat: copy or concatenate at most $n$ bytes (characters)
char firstName = "Christopher";
char name[6];
strncat(name, firstName, 5);
//often, you may need to handle the null terminator yourself:
name[5] = '\0';
  • With both, they'll handle the null terminating character IF and only if it appears within the first $n$ characters of the source string
  • both copy at most $n$ characters: they'll stop when they see that first null-terminator
  • Using the referencing operator, you can also copy a "substring"
char fullName[] = "Christopher Michael Bourke";
char middleName[8];
//want to copy "Michael" into middleName
strncpy(middleName, &fullName[12], 7);
middleName[7] = '\0';
printf("middle name = %s\n", middleName);

String in Java

  • In Java, strings are full objects and defined by the class String
  • There is NO null-terminating character in Java
  • There is no dynamic memory management so just use strings however you want!
  • Strings are immutable
String message = "hello World!";
message = "Hello World!";
  • Consequently, any string method that "changes" the contents of a string, actually returns a NEW string
String s = "hello";
s.toUpperCase();
//no effect, s is still "hello";
String t = s.toUpperCase();
System.out.println(s);
System.out.println(t);
  • Strings are immutable, but you have mutable version called StringBuilder









Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment