Strings

0 kendratovar357 November 25, 2020

Strings are an array of characters i.e they are characters arranged one after another in memory. Thus, the character array is also known as a string. Each character within the string will be stored within one element of an array. A string is used by C++ to manipulate text such as words and characters.

Initializing Strings

A string is initialized as below

char name[] = {'R', 'A', 'M'};

This method is also valid but C++ provides another way to initialize string as

char name[] = "RAM";

The characters of the string are enclosed within a pair of double-quotes.

Now we will look at an example to take input from the user as a string and display string as well using printf() and scanf().

#include <stdio.h>
#include <string.h>
int main()
{
    /* String Declaration*/
    char nickname[20];

    printf("Enter your Nick name:");

    scanf("%s", nickname);

    /*Displaying String*/
    printf("%s",nickname);

    return 0;
}

The output of the above program is:

Enter your Nick name: Suresh
Suresh

Note: %s format specifier is used for strings input/output

String Handling Functions

The library or built-in functions strlen()strcpy() etc are used for string manipulation in C. These functions are defined in header file string.h., They are described in detail below.

  • strlen(): return length of string
  • strcpy(): copies one string to another
  • strcat(): concatenates two strings
  • strcmp(): compare two strings to find out whether they are the same or different.
  • strrev(): reverse all the characters in a string expect null character at the end of the string

1. strlen()

This function returns an integer which denotes the length of the string passed. Its syntax is

integet_var = strlen(string);

Let’s look at one example to make clear concepts on strlen().

#include <stdio.h>
#include <string.h>
int main()
{
     char str[20] = "CodeHelpPro";
     printf("Length of string: %d", strlen(str));
     return 0;
}

The output of the above program is:

Length of string: 11

2. strcpy()

The strcpy() functions copy one string to another. The function accepts two strings as parameters and copies the second string character by character into the first one up to and including the null character of the second string. Its syntax is:

strcpy(s1, s2);

Let’s look at one example to make clear concepts on strcpy().

#include <stdio.h>
#include <string.h>
int main()
{
     char s1[30] = "string 1";
     char s2[30] = "string 2 : I’m gonna copied into s1";
     strcpy(s1,s2);
     printf("String s1 is: %s", s1);
     return 0;
}

The output of the above program is:

String s1 is: string 2: I’m gonna copied into s1

3. strcat()

This function concatenates two strings i.e. it appends one string at the end of another.

strcat(string1, string2);

Let’s look at one example to make clear concepts on strcat().

#include <stdio.h>
#include <string.h>

int main
{
    char firstName[20] = "Suresh",
             lastName[20] = " Chand";

    strcat(firstName, lastName);

    printf("Full name is %s", firstName);
    return 0;
}

The output of the above program is:

Full name is Suresh Chand

4. strcmp()

This function compares two strings to find out whether they are the same or different. This function is useful for constructing and searching strigs as arranged in a directory. this function accepts two strings as parameters and returns n integer whose value is

  • less than 0 if the first string is less than the second
  • equal to 1 if both are the same
  • greater than 1 if the first string is greater than the second

Let’s look at one example to make clear concepts on strcmp().

#include <stdio.h>
#include <string.h>

int main()
{
    
    char name1[15], name2[15];
    int diff;

    printf("Enter first string : ");
    gets(name1);

    printf("Enter second string : ");
    gets(name2);

    diff = strcmp(name1, name2);
    if(diff > 0){
        printf("%s is greater than %s by the value %d.", name1, name2, diff);
    }else if(diff > 0){
        printf("%s is less than %s by the value %d.", name1, name2, diff);
    }else{
        printf("Bothstring are equal.");
    }

    return 0;
}

The output of the above program is:

Enter first string : Suresh
Enter second string : Chand
Suresh is greater than Chand by the value 1.

5. strrev()

This function is used to reverse all characters in a string except the null character at the end of the string. Its syntax is

strrev(string);

Let’s look at one example to make clear concepts on strrev().

#include <stdio.h>
#include <string.h>

int main()
{

    char name[15] = "Suresh chand", name2[15];
    strcpy(name2, name);

    strrev(name);
    printf("The reverse string of %s is %s", name2, name);

    return 0;
}

The output of the above program is:

The reverse string of Suresh chand is dnahc hseruS