Array of String

Definition

An array of characters is called string.

Ex. One wants to store the name “RAMAN”, then he wants 5 char variables. “R” will be stored in 1st char variable, “A” in 2nd char variable and so on. So to store the whole name “RAMAN” in one variable, we can use char array up to index(minimum) 5. String by default stored NULL character ‘\0’ at the end of the string.

Methods to give value to a string

There are two methods to give value to a string and those are listed below.

  • Method to give value at compile time

In this method, we give value to a string at the time of declaring a string. In this also we can use 3 methods. In the first and second methods, we have to use a curly bracket at outside.

Way 1.

char str[4]={“VVP”};

In 1st way, we can write the all characters (in form of one word) in double quotes. In this method, at last the NULL character (\0) is added automatically.

Way 2.

char str[4]={‘V’,’V’,’P’,’\0’};

In 2nd way, we are separating all characters using comma(,) and all characters are written separately in single quotes. In this method, at last the NULL character (\0) is not added automatically. So we have to add it separately at the end using ‘\0’.

Way 3.

char str[]=”VVP”;              // in this method, there is no need to write index

In 3rd way, there is no need to write curly bracket at outside and no need to write index. Instead of that, we have to write just one pair or square bracket []. In this method, we just have to write all characters in form of one word in double-quotes. This method also adds NULL character automatically at the end.

  • Method to give value at run time

The below example shows how to take value from the user at runtime of string using scanf. Generally, we write variable name with “&” sign in scanf. But for string, we do not write “&” sign in scanf. For ex1,  in this method, in scanf, there is no need to write ‘&’ sign before string name because when we write str, it means &str[0] itself. scanf does not allow to take the value with space. This means if we give value ”abc de” then it takes value “abc” only

Ex1.

char str[10];

scanf(“%s”,str);

This 2nd method is same as first method but we are using gets instead of scanf. In 2nd  method also , there is no need of sign ‘&’. gets() function takes string value with space also. Means if we give value ”abc de” then it takes value “abc de”

Ex2.

// In this method also, there is no need of sign ‘&’
char str[10];

gets(str);

As gets is used to take the value of a string, puts is used to print the value of a string

Syntax of gets() and puts()

To use gets() and puts(), we have to include conio.h library file.

 gets(<name of string variable>);

To use gets, we just have to write gets keyword and then write the name of a string variable in the round bracket whose value we want to get by the user. The difference between using gets and scanf to take a value of string from the user is that in scanf, we can not give value with space but in gets, we can.

 puts(<name of string variable>);

To use puts, we just have to write put keyword and then write the name of a string variable in round bracket whose value we want to print.

Example program for how to use gets and puts

In the below example, we take the value of string str using gets. gets can take the value of string with space. This shows output if the value given by the user is “c language”. Here str is string of 20 array elements. We are asking user to give value of this string using gets function and then that string value is printed at output using puts.

#include<stdio.h>
#include<conio.h>
void main()
{
  char str[20];
  clrscr();
  printf(”enter value of string : ”);
  gets(str);
  printf(“string is : “);
  puts(str);
  getch();
}

Output:

Example program for how to use gets and put

In the below example, we take the value of string str using scanf. scanf can not take the value of string with space. In the below example 1st output is for example when the user is giving value “abc def” and 2nd output is when the user is giving value “abcdef”.

#include<stdio.h>
#include<stdio.h>
void main()
{
  char str[20];
  clrscr();
  printf("enter value of string : ");
  scanf("%s",str);
  printf("string1 is : %s",str);
  getch();
}

Output with input having Space:

Output without input having Space:

Learning from this blog:
1 ) When to use string
2 ) Different ways to assign values to string
3 ) Difference between use of gets() and scanf()
4 ) Program of how to get the value of string using gets(),scanf() and display it
5 ) Use of printf() and puts() in string

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments