C Programming

Learning Strings in C

A string is an important concept in the C language. A string is nothing but an array that contains only character type value. So we can say indirectly that string is a character array. A sequence of characters terminated at null character.

ASCII code of null character is 0.

Programming Example 1

In this programming, we will try to learn how a string is declared and initialized, then print all the characters of that particular String.

#include<stdio.h>

int main()
{
// declaration and initialization of string.
    char s[10]  = { 'S' , 'A' , 'U' , 'R' , 'A' , 'B' , 'H' , '\0' } ;
    for ( inti = 0 ; i<= 7 ; i++ )
    {
printf( " %c ",s[i] );  // printing the string
    }
printf( " \n " ) ;
  return 0 ;
}

Output

Note

If we want to initialize the string, we can write each letter of a word with is ‘ & ‘ separate by, and end with a special character ‘ \0 ‘.

Explanation

Here we want to print the null character, but we can’t able to print the null character because it is not a printable character, but it has some other usefulness.

If we want to take a value or string from the user, then we don’t know how many times the loop will run, but when the loop sees that ‘ \0 ‘has come, then it will understand that this is the end of the string.

for ( i = 0 ; s[ i ]!= '\0'; i++) // usage of null character

printf ("%c ", s [ i ] ) ;

If we don’t want to print each char of a string usingLoop and want to print the total string, then-

printf ( " %s ", s ) ;// Instead of loop to print the string

Another Function can be used to print the string.

puts ( s ); // [No Format specifier is required]

[puts () function automatic change the line]

puts (&s[ 0 ] ) ;

Output

SAURABH

Another Format of String Initialization

char s [10] = “ SAURABH “

If we don’t want to initialize the string at the time of declaration, want to take a string from the user, then –

printf (“enter name“ );

1. scanf ( “ %s ” , s ) ; // Here don’t require to use and sign .

2. scanf (“ %s” , %s [ 0 ]) ; // the meaning of two lines are same

scanf() has only one drawback. If we want to print a name like Avishek Dutta, Then it only prints Avishek. Because scanf() is like a delimiter of separation. If we use it, then it understands that information or string is finished. It has another form –

gets ( s ); -- It takes a string from the user just like scanf().

But it prints the total string – Avishek Dutta.

Programming Example 2

This programming example will teach how we declare and initialize a string with the help of the malloc () function.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main()
{
    char *a ;
    int b ;

printf( " How many characters in the string ? " ) ;
scanf("%d", &b);
    a = ( char* ) malloc ( b * sizeof ( char ) ) ;      // creating the block of string
printf( " Insert the string : " ) ;
scanf("%s", a);
printf( " Entered string : %s \n ", a ) ;
    free ( a ) ;
    return 0 ;
}

Output

Explanation

Here we want to input a string from the user. The string is created with the help of dynamic memory allocation. malloc() create the string and returns the address of the string to the variable a. Now we simply print the string.

String Related Function

strlen ( )

Passing the address of the string & it returns the length of the string. Passing only one argument.

strrev ( )

Reverse the string. It only takes one argument that is the address of the string & returns the reverse string.

strlwr ( )

It changes the upper case letter to the lower case letter.

strupr ( )

It changes the lower case letter to the upper case letter.

strcpy ( )

To copy the string

strcpy ( s “ HABIBPUR “ )

strcmp ( )

We can do below to compare the string.

strcmp ( “ AMAR “ , “ AMIT “ )

It returns one integer value that is a corresponding mismatch of the ascii character.

strcat ( )

To append the two strings,

strcat (s , “ student “ ) ; s [ ] = “ HELLO “

So, it is connected as “HELLO student “.

Handling multiple of string:

char s [ 3 ] [ 10 ] = { “ BHOPAL “ , “ DELHI “ , “ KANPUR “ } ;

[3] Means how many string we can put in these 2D string.

Programming Example 3

In this programming, we are learning how the two-dimensional string is declared and initialized and its application.

#include<stdio.h>

int main()
{
    char s[3][10]  ;
printf( " enter three strings " ) ;
    for ( inti = 0 ; i<= 2 ; i++ )
    {
        gets(&s[i][0]);
    }
    for ( inti = 0 ; i<= 2 ; i++ )
    {
printf( " %s \n ",s[i] );
    }
    return 0 ;
}

Output

Explanation

Like an array, we can declare and initialize a two-dimensional string in the C language. Here we declare a 2D string named s [3][10]. Within brackets 3 and 10 means we assume that this particular string has three rows, and each row has ten columns. In the string, we assign three cities’ names. They are Bhopal, Delhi, and Kanpur. These values are taken from the user by using the gets () function through for loop. Now we simply print the cities name.

Conclusion

From the above discussion about string, we have come to the decision that String is a powerful concept in C language. String gives us a new dimension to store character type values. Through different functions in the string, we can operate different types of character values to concatenate, lower to capital, capital to lower, reverse, etc.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com