strlen C : Counting Strings
Strings in C Programming Language
Strings are essential elements in a programmer’s toolkit. They are one of the most commonly used elementary data types, while writing code, and debugging during the development; as also while formatting output for the user.
In this blog post we are going to see how you can create strings, write them to the output, read user input as strings, and count them. Compared to modern programming languages like Python, or Java, string handling in C is still primitive.
String Operations
Strings in C language are all NULL terminated. NULL is the character ” which is part of the ASCII and extended ASCII sets.
This article is fully concerned with the ASCII base 0-127 values of the 7-bit character set. For internationalization applications, you will need wide-characters, and you will worry about the encoding. But these are not concerns now; we settle with ASCII encoding and basic ISO-Latin/English scripts.
Setting Up Your Environment
If you are a beginning C user, you may benefit from using the C compiler in small steps. In Windows, you can use the Mingw GNU tool chain, Borland C/C++ compilers or Microsoft Visual Studio, or the shell in Linux. Start by typing in the shell,
$ gcc --version
and you should see the following output, then you are ready to dive into C development.
$ gcc --version gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 Copyright (C) 2011 Free Software Foundation, Inc.
String API in C
To include the string input-output operations in your program, you can add the header file definition:
#include <string.h>
where the strings are known to be NULL terminated character arrays. You can declare a string as follows in the C language
char roman_emperor_name[] = “Julius Caesar”;
and using the builtin sizeof, and strlen function we can print the memory size of string, and the string length. The ‘strlen’ function defined in the header ‘string.h’ and takes as argument a pointer to a characters array which it reads till the NULL character, and returns the count upto this point, not including this.
Together, our code listing for ‘string_demo0.c’ is the following,
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { char roman_emperor_name[] = "Julius Caesar"; printf(" Length of string '%s' = %d\n",roman_emperor_name,strlen(roman_emperor_name)); printf(" number of bytes for storing string '%s' = %d\n",roman_emperor_name,sizeof(roman_emperor_name)); return 0; }
Compiling and running the program you should see the terminal message,
Length of string 'Julius Caesar' = 13 number of bytes for storing string 'Julius Caesar' = 14
For more details read the manual page on Linux for assert function.
$ man strlen
should bring up a page like,
STRLEN(3) Linux Programmer's Manual STRLEN(3) NAME strlen - calculate the length of a string SYNOPSIS #include <string.h> size_t strlen(const char *s); DESCRIPTION The strlen() function calculates the length of the string s, excluding the terminating null byte ('').
Related functions
You can also compare strings, copy strings fully or upto n-characters, duplicate strings and use routines to check if characters are alphabetic, numeric, space etc. For details pull up the following manual page by typing, $ man strcpy,
STRCPY(3) Linux Programmer's Manual STRCPY(3) NAME strcpy, strncpy - copy a string SYNOPSIS #include <string.h> char *strcpy(char *dest, const char *src); char *strncpy(char *dest, const char *src, size_t n); DESCRIPTION The strcpy() function copies the string pointed to by src, including the terminating null byte (''), to the buffer pointed to by dest.
Creating Runtime Strings – Memory Allocation
To create strings on the run time, say from data received over the Internet, or user input, or files read off the disk, we need to know the number of bytes you want to store on memory. Since this information is not available at runtime, we have to allocate memory and create new space.
In this routine get_new_user_info, we use the function malloc, (for which use the directive #include<stdlib.h>), which retrieves usable memory space from the OS heap memory.
In the following example we request memory for a 500 bytes string which we need at run time. Then we make a local copy of the input string upto 500 bytes, and return a new copy back to the user.
char* make_upto500char_copy( char* old_str ) { char* str500byte = malloc( sizeof(char)*500 ); strncpy( str500byte, old_str, 500 ); return str500byte; } Using some boiler plate main() function to drive this routine, we have a program listing for the code, 'str_demo0.c' as followings. #include<stdio.h> #include<string.h> #include<stdlib.h> char* make_upto500char_copy( char* old_str ) { char* str500byte = malloc( sizeof(char)*500 ); strncpy( str500byte, old_str, 500*sizeof(char) ); return str500byte; } int main() { char *old_str = "Hello, Hello, we are who we are..."; char *new_copy = make_upto500char_copy( old_str ); new_copy[0] = 'J'; //change first letter printf("Old = %s\nNew = %s\n",old_str,new_copy); free(new_copy); return 0; }
When you compile the code, and then run program, you will see the following output,
Old = Hello, Hello, we are who we are... New = Jello, Hello, we are who we are...
Example – Listing Length Of Input Strings
In this example we read the user input at the console, and print out the statistics of the line number and the line length. Our length includes the ‘\n’ or EOL character, which is why it is 1 more than actual character size in it. We make use of the function getline() in Linux to read the user input stream and output the data.
The program listing, ‘strlen_readip.c’,
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { char *input_line = NULL; int line_no = 0, read_count = 0; unsigned int data_length=0; while( getline(&input_line,&read_count,stdin) != - 1 ) { printf( "### %d | L = %d | %s \n >>>",++line_no,strlen(input_line),input_line); } return 0; }
on compiling and giving the following input interactively, gives the output as below
what is your name ### 1 | L = 18 | what is your name >>>well this is elementary. ### 2 | L = 25 | well this is elementary. >>>Ok Bye ### 3 | L = 6 | Ok Bye ^D
Summary
Software developers use strings pervasively in code. Strings are essential elements in a programmer’s toolkit. They are one of the most commonly used elementary data types, while writing code, and debugging during the development; as also while formatting output for the user. You can learn more about algorithms and data structures, when you have mastered the basics of C language.
Recommended Articles
Top courses in Development
Popular topics
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.