Strings in C: How to Declare & Initialize a String Variables in C 2022

What is String in C?

A String in C is only an assortment of characters in a straight grouping. ‘C’ consistently treats a string of solitary information despite the fact that it contains whitespaces. A solitary person is characterized utilizing a single statement portrayal. A string is addressed utilizing twofold statement marks.

Strings are only one of the many structures in which data is introduced and gets handled by PCs. Strings in the C programming language work uniquely in contrast to in other current programming dialects. In this article, you’ll figure out how to proclaim strings in C. Prior to doing as such, you’ll go through an essential outline of what information types, factors, and clusters are in C. Thusly, you’ll see how these are completely associated with each other with regards to working with strings in C. Knowing the rudiments of those ideas will then, at that point, assist you with bettering see how to announce and function with strings in C.

Model, “Welcome to the universe of programming!”

‘C’ gives standard library <string.h> that contains many capacities which can be utilized to perform muddled tasks effectively on Strings in C.

Method 1:

char address[]={'T', 'E', 'X', 'A', 'S', '\0'};

Method 2: The above string can also be defined as –

char address[]="TEXAS";

In the above declaration NULL character (\0) will automatically be inserted at the end of the string.

What is NULL Char “\0”?
'\0' represents the end of the string. It is also referred as String terminator & Null Character.

Read & write Strings in C using Printf() and Scanf() functions

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

    printf("Enter your Nick name:");

    /* I am reading the input string and storing it in nickname
     * Array name alone works as a base address of array so
     * we can use nickname instead of &nickname here
     */
    scanf("%s", nickname);

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

    return 0;
}

Output:

Enter your Nick name:Negan
Negan

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

Read & Write Strings in C using gets() and puts() functions

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

    /* Console display using puts */
    puts("Enter your Nick name:");

    /*Input using gets*/
    gets(nickname);

    puts(nickname);

    return 0;
}

C String function – strlen

Syntax:

size_t strlen(const char *str)

size_t represents unsigned short
It returns the length of the string without including end character (terminating char ‘\0’).

Example of strlen:

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

Output:

Length of string str1: 13

In order to read a string contains spaces, we use the gets() function. Gets ignores the whitespaces. It stops reading when a newline is reached (the Enter key is pressed).

For example:

#include <stdio.h>
int main() {
char full_name[25];
printf("Enter your full name: ");
gets(full_name);
printf("My full name is %s ",full_name);
return 0;
}

Output:

Enter your full name: Dennis Ritchie
My full name is Dennis Ritchie

Another safer alternative to gets() is fgets() function which reads a specified number of characters.

For example:

#include <stdio.h>
int main() {
char name[10];
printf("Enter your  name plz: ");
fgets(name, 10, stdin);
printf("My name is %s ",name);
return 0;}

Output:

Enter your name plz: Carlos
My name is Carlos

The fgets() arguments are:

  • the string name,
  • the number of characters to read,
  • stdin means to read from the standard input which is the keyboard.

Other important library functions are:

  • strncmp(str1, str2, n) :it returns 0 if the first n characters of str1 is equal to the first n characters of str2, less than 0 if str1 < str2, and greater than 0 if str1 > str2.
  • strncpy(str1, str2, n) This function is used to copy a string from another string. Copies the first n characters of str2 to str1
  • strchr(str1, c): it returns a pointer to the first occurrence of char c in str1, or NULL if character not found.
  • strrchr(str1, c): it searches str1 in reverse and returns a pointer to the position of char c in str1, or NULL if character not found.
  • strstr(str1, str2): it returns a pointer to the first occurrence of str2 in str1, or NULL if str2 not found.
  • strncat(str1, str2, n) Appends (concatenates) first n characters of str2 to the end of str1 and returns a pointer to str1.
  • strlwr() :to convert string to lower case
  • strupr() :to convert string to upper case
  • strrev() : to reverse string

Converting a String to a Number

In C programming, we can convert a string of numeric characters to a numeric value to prevent a run-time error. The stdio.h library contains the following functions for converting a string to a number:

  • int atoi(str) Stands for ASCII to integer; it converts str to the equivalent int value. 0 is returned if the first character is not a number or no numbers are encountered.
  • double atof(str) Stands for ASCII to float, it converts str to the equivalent double value. 0.0 is returned if the first character is not a number or no numbers are encountered.
  • long int atol(str) Stands for ASCII to long int, Converts str to the equivalent long integer value. 0 is returned if the first character is not a number or no numbers are encountered.

The following program demonstrates atoi() function:

#include <stdio.h>
int main()
{char *string_id[10];
  int ID;
  printf("Enter a number: ");
  gets(string_id);
  ID = atoi(string_id);
   printf("you enter %d  ",ID);
  return 0;}

Output:

Enter a number: 221348
you enter 221348
  • A string pointer declaration such as char *string = “language” is a constant and cannot be modified.

Summary

  • A string is a sequence of characters stored in a character array.
  • A string is a text enclosed in double quotation marks.
  • A character such as ‘d’ is not a string and it is indicated by single quotation marks.
  • ‘C’ provides standard library functions to manipulate strings in a program. String manipulators are stored in <string.h> header file.
  • A string must be declared or initialized before using into a program.
  • There are different input and output string functions, each one among them has its features.
  • Don’t forget to include the string library to work with its functions
  • We can convert string to number through the atoi(), atof() and atol() which are very useful for coding and decoding processes.
  • We can manipulate different strings by defining a array of strings in C.

Also Read: How To Create a Countdown Timer Using Python?

Leave a Reply

Your email address will not be published. Required fields are marked *