You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Strings represent data that may need to be processed
Common to deal with CSV (Comma separated value) or similar formatted data
Standard library functions can help, but processing involves designing algorithms
More Convenience functions in C
There is a ctype.h library that provides several "check" functions for single characters: isalpha(c), isdigit(c), islower(c), isupper(c), isspace(c), conversion: toupper(c), tolower(c), etc.
String Formatting
In C: you can use printf to print a formatted string to the standard output
You can use sprintf to "print" to a string instead
You use it exactly the same as printf but it takes an additional first argument: the string you want to "print" to
It is YOUR responsibility to ensure the string is big enough
chars[100];
intx=10;
doublepi=3.1415charname[] ="Chris";
//standard output:printf("Hello, %s, you have a value of %d, and pi is %.3f\n",
name, x, pi);
sprintf(s, "Hello, %s, you have a value of %d, and pi is %.3f\n",
name, x, pi);
char*result= (char*) malloc(sizeof(char) * (strlen(s) +1));
strcpy(result, s);
//now I have a string, result of the EXACT size I need~!
General strategy: you can use a "temporary buffer" like above to format a string and then once formatted, you can use strlen and malloc to create a string of the exact size you need, then copy it over, and return it if needed
Java
Java: String.format which uses a printf- style placeholders and returns a String
intx = 10;
doublepi = 3.1415Stringname = "Chris";
Strings = String.format("Hello, %s, you have a value of %d, and pi is %.3f\n",
name, x, pi);
Java: Mutable Strings
In Java the String class is immutable
In general, immutability is a Very Good Thing: immutable objects are automatically thread-safe
However, sometimes you might need a mutable version: StringBuilder: you can change the contents, it shrinks/expands automatically
Some strings may contain formatted data: CSV, TSV (tab separated values)
We want to "split" a formatted string up into tokens along some delimiter (tab, space, commas)
In C: you can use strtok, it takes two arguments: the string you want to tokenize and a string containing one or more delimiters
The first time you call it, you pass the string you want to tokenize
Each subsequent call, you pass in NULL in order to continue tokenizing the same string
strtok returns a pointer to the next token, NULL when there are no more tokens
You can use multiple delimiters, but generally stick with one
chars[] ="Chris,Bourke,UNL,Omaha,NE";
//strtok returns a pointer to the next token, so we need to store that:char*token=NULL;
//first argument is the string you want to tokenize//second argument: the delimiter//it returns a pointer to the "next' tokentoken=strtok(s, ",");
//the pointer returned, points to a part of the tokenized string//with the delimiter *replaced* with a null termianting character//print it:printf("The first token is %s\n", token);
//next token: passing in NULL tells strtok to continue tokenizing// where it left offtoken=strtok(NULL, ",");`
printf("the second token is %s\n", token);
//how do we continue?//When no further tokens are available, strtok returns NULLwhile(token!=NULL) {
printf("token = %s\n", token);
token=strtok(NULL, ",");
}
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)
charfirstName="Christopher";
charname[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"
charfullName[] ="Christopher Michael Bourke";
charmiddleName[8];
//want to copy "Michael" into middleNamestrncpy(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!