Strings in C


Nowadays, Computers are widely used for Word Processing applications for creating, inserting , modifying and manipulating textual data. Besides this, we need several programs for manipulating textual data, like finding specific patterns etc. In C programming also we deal with textual data and we need to manipulate it properly, so we are going to talk about a special category of arrays , known as Strings. 
Strings are character type 1-Dimensional arrays ending with Null character ('\0'). That means is we write a string "Hello" , then after 'o' ,there will be another character known as the NULL character, at the end. This '\0' (NULL character) signifies the end of the string.The compiler automatically assigns the '\0' to the end of string, also we can do it manually.

Declaring a String 

A string can be easily declared using the following format :

char array_name[size] = {string_elements}; 

  • Here arrays_name can be any name defined to the array
  • We can specify the SIZE of the string or else we can leave the size portion empty, as the compiler automatically calculates the size, depending upon the number of elements in the string.
  • string_elements will contain the characters of the text we filled, like, "hello" or  {'h','e','l','l','o'}.
We always need not enter the text at the declaration, we can do it later on also, using loops.
We can just declare the array as follows, but in this case we need to specify the size :

char str[100];

Initializing a String

We can initialize strings in different ways :

char str[] = "Hello" ;  //In this case the NULL character will be inserted automatically by the compiler

char str[] = {'H','e','l','l','o','\0'};

We can also initialize them during runtime ,by using scanf() function or the gets() function included in the <stdio.h> header file. gets() function reads the whole string as an input from the user, also if we want to read each single character one by one, we can use the getchar() function. It reads one character at a time.
With scanf() , %s specifier is used to input a string.

Ques. Input a string using gets() function.

#include<stdio.h>
#include<conio.h>
int main()
{
char str[100];
printf("Enter a String :");
gets(data);

printf("Entered Data is:");
puts(data);
        return 0;
}

OUTPUT 

Enter a String:Hello
Entered Data is:Hello


The puts() function is used to print the string, but unlike printf() function it appends a new line automatically at the end.

Ques. Input a string using getchar() function.

#include <stdio.h>
int main()
{
 char str[50], ch;
 int i;
 printf("Enter a string: ");
 i = 0;
 ch = getchar ();
 while(ch!='\n')
  {
   str[i] = ch;
   i++;
   ch = getchar();
  }
 str[i] ='\0';
 printf("Entered string is: %s", str);
 return 0;
}

OUTPUT

Enter a string: Hello
Entered string is: Hello

This program will keep taking the input until the while loop condition gets false, that is, until the user presses enter.


One of the major drawback of using scanf() function for printing strings is that, if our string contains 2 words like "hello world", then the scanf() function will scan only "hello" and ignore the rest of the string.This is because the moment blank space is encountered, the string is terminated. To encounter this we can specify the field width, to indicate the maximum number of characters that can be read in.
like, scanf("%7d",str); ,here it will read only 7 characters and also while using scanf() to read the string we need not put the & sign infront of the variable name, because in pointers we studied that the name of the string is the base address itself,which contains the address of the whole array.

>> To solve this problem with scanf, we can use [^\n] ,it tells the compiler to take input untill its not a newline (\n).

#include <stdio.h>
int main()
{
 char str[50], ch;
 int i;
 printf("Enter the string\n");
scanf("%[\n]s",str);
 printf("Entered string is: %s", str);
 return 0;
}

OUTPUT

Enter the string
Hello World
Entered string is: Hello World


Ritish

Just a novice blogger

Post a Comment (0)
Previous Post Next Post