Getting Started With C Strings
In the C programming language, a string is merely an array of characters that has a length determined by a null terminating character ‘’. Other programming languages like Java and C# have string data types but C doesn’t. To work with them in C, you need to treat them as characters in an array.
Overview of C Programming
C is one of the most popularly used programming languages of all time. It is a general-purpose programming language that has facilities for structured programming and imperative languages. It is a procedural language, designed for straightforward compiling to provide low-level access to memory.
Strings in C
There really isn’t a string data type in C. Instead, the primitive data type char is used in arrays. Let’s say you have a string “sample” which has 6 characters. In C, it would simply be an array containing the characters, ‘s’, ‘a’, ‘m’, ‘p’, ‘l’, ‘e’.
In C Programming, initializing a string, can be done in many ways:
First, you declare your data type as char, and then you name the variable. You leave the brackets empty because you are initializing the value on the other side of the equal sign, which then determines the length for you. You use double quotation marks (“) to denote that all the characters within are part of the string.
Basically the same as the one above, but here you are explicitly declaring the length of your string by putting 6 inside the brackets.
In the above example, single quotation marks are used (‘). But in doing so, you need to separate each character and enclose only one within each quote pair. The brackets are again left empty to let the initialized value determine the length.
Also, the same as the example above with the single quotation marks, but this one has an explicit declaration of the character array length
Reading C Strings
Strings are not always pre-initialized like the previous examples. What if they need to come from the user? To read strings as user input, use this code:
char s; scanf("%s", s);
First, you declare the string variable and give it a size. Then, you use the scanf function. Within the scanf() function, you use ‘%s’ to denote that the value you’ll be accepting is a string. After this, add the variable (‘s’) in which you’ll be storing the value from the user input.
However, keep in mind that the scanf function on its own will only read characters before any white space. If you type in a string “This is a sample,” the only text to be stored in variable s will be “This”.
To read a full line of text, you may use the predefined function gets().
char sample; printf("Enter sample text: "); gets(sample);
Printing C Strings
To display strings in C, all you have to do is use the conversion specification, ‘%s’ in the printf() function.
printf("Sample: %s", sample);
The gets() function also has a counterpart for the printf() function, which is the puts() function.
printf("Sample: "); puts(sample);
String Handling Functions
Strings are not always simply initialized or read. There will be times that the programmer will need to manipulate the value of the string according to the problem being solved. String manipulation can actually be done manually. However, this is very tedious work for the programmer. And because this task is very common to many C programs, a library has been created to make string handling more convenient and efficient.
In C Programming, you will first need to include the <“string.h”> library to make use of the string handling functions that can help you examine and manipulate C strings with ease. The methods in the said library include:
strlen() – This method returns the length of the string. It accepts the string as a parameter and returns the length in terms of the size of the character array.
int length = strlen("sample");
strcpy() – This method copies a string into another string. The first parameter (string1) is the destination of the string to be copied from the second parameter (string2).
strcat() – This is the string concatenation method. To concatenate means to join 2 strings. The second parameter will be added to the first parameter.
strcat(@”Hel”,”lo”); //The final string will be “Hello”
strcmp() – This method compares the strings that you pass as its parameters. It returns an integer value that denotes the difference of the string literals.
int comp = strcmp(string1,string2);
If the value returned is 1, then the content of string1 is greater than the contents of string2. If the value is -1, then the content of string2 is greater than string1. But if the value is 0, then the strings in string1 and string2 are equal.
strlwr() – This method converts the given string into lowercase.
If the value of string1 is “HELLO”, after it is passed to the strlwr() method, its value would become “hello”. This method works by invoking tolower() for each character in the string.
strupr() – This method is the opposite of the strlwr method. Instead of converting the text into lower case, it converts it to uppercase string.
In this case, if the value of string1 is “hello”, the strupr() method will convert it to “HELLO”.
There are many other C String library methods that you can choose from for the different tasks that you need to accomplish using the C Programming language. If you are working on complicated problems that require lower levels of string and memory manipulation, always remember that strings in C are merely arrays of character data and these characters can be treated the same way you would with any other data arrays.
Top courses in C#
C# students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.