Strings in C/C++

Basics of C/C++ Programming

Strings in C/C++

For the past week I've been making an effort to understand ‘Strings’ which is also the topic of today’s post and the next topic in our series of ‘C fundamentals’. After reading this post you will get a clear understanding as to what is a string and of its functionalities.

  1. Firstly, let’s answer what a string really is? Strings in C is a sequence or array of characters . Only difference b/w string and a character array is that string ends with ‘\0’(also known as Null Terminator).

  2. String Declaration- char str_name[str_size] Ways of Initialising a String:- char str_name[str_size]= “String”
    char str_name[]= “String” char str_name[7]= “String” char str_name[]={‘S’, ‘t’, ‘r’, ‘i’, ‘n’, ‘g’,’\0’}

(Note:-Destination and source here means destination string and source string respectively.)

3.Now since we know how to declare and initialise a string,we should now move towards some of the functions that come into use while using or working with strings. These functions are available in the string.h library.

  • Strcpy- String copy function as the name suggests is used to copy one string to another string. Its syntax is strcpy(destination,source). More useful version of strcpy is strncpy in which size of the string has to be specified.

  • Strlen- String length function is used to get the length of the string. Its syntax is strlen(string_name).

  • Strcat- String concatenates(joins) two strings. Its syntax is strcat(char source,const char destination).

  • Strcmp- String compare is used to compare two strings to identify which one is longer. Its syntax is strcmp(str1,str2) (Note:-It returns 0 if both strings are equal,>0 if str1>str2,<0 if str1<str2.)

For detailed implementation and functioning of the above visit my Github Repository:- github.com/harsh007-github/Fundamentals-of-..

Thank you for reading till here