STRING IN C

📚 Topics Covered

WHAT IS A STRING?

DEFINITION:

A string is a group of characters. When you create a string the last character will be \0.
A string is stored inside the character array. You can about it as a single character, group of
characters creates a string, group of strings creates a sentence, and group of sentences creates a
paragraph and group of paragraphs creates a LESSON!!

real life example of a variable

In the above image, the representation of string is displayed in form of visuals.

EXAMPLE: HOW TO CREATE A STRING


char fullName[50] = "Intelle Learn";
    

EXAMPLE:


#include stdio.h

int main()
{
	char fullName[50];

	printf("Enter your full name: ");
	scanf("%s",&fullName);

	printf("Full Name entered is: %s\n",fullName);
	return 0;
}


    

OUTPUT:


Enter your full name: Intelle Learn
Full name entered is: Intelle
    

In the above program, did you noticed something? The value which we stored inside the fullName is "Intelle Learn"
But what is the value displayed?


Full name entered is: Intelle

By default printf() considers a space as a termination of the value means the value inside the variable is completed.
To take Input & Output of the whole string on the screen we are going to use Input & Output Function of String.

INPUT & OUTPUT FUNCTION IN STRING

WHAT IS INPUT FUNCTION IN STRING?

gets() function is used to take input value as a string and store it inside the variable.

SYNTAX:


gets(string Variable);
    

WHAT IS OUTPUT FUNCTION IN STRING?

puts() function is used to display the value of the String variable.

SYNTAX:


puts(string Variable);
    

NOTE:

gets() and puts() are defined in the stdio.h standard library.

ËXAMPLE:


#include stdio.h

int main()
{
	char fullName[50];

	printf("Enter full name: ");
	gets(fullName);
	printf("Full name entered is: ");
	puts(fullName);

	return 0;
}
    

OUTPUT:


Enter full name: Intelle Learn
Full name entered is: Intelle Learn
    

STRING FUNCTIONS IN C

HOW TO USE THE STRING FUNCTION?

There are many string functions which we use in C programming. Some of the functions are:

All these string manipulation functions are inside the string.h header file.

STRING IN POINTER

HOW STRING IS USED IN POINTER?

We used string in the pointer which can access each of the character with the help of pointer.

EXAMPLE:


#include stdio.h

int main()
{
	char fullName[] = "Intelle Learn";
	char *pointer1;
	pointer1 = fullName;

	printf("%c\n",*pointer1);	//Output: I
	printf("%c\n",*(pointer1 + 2));	//Output: t
	printf("%c\n",*(pointer1 + 6));	//Output: e 

	return 0;
}

OUTPUT:


I
t
e

PASS STRING IN FUNCTION

HOW TO PASS STRING IN FUNCTION?

In this topic we will understand how string is used as an argument in the function.

EXAMPLE:


#include stdio.h

void displayName(char name[]);

int main()
{
	char fullName[50];

	printf("Enter your Full Name: ");
	gets(fullName);
	displayName(fullName);

	return 0;
}

void displayName(char name[])
{
	printf("Full Name entered is: ");
	puts(name);
}

OUTPUT:


Enter your Full Name: Intelle Learn
Full Name entered is: Intelle Learn 

Video Explanation: DYNAMIC MEMORY ALLOCATION IN C

If video not loading,watch on YouTube

Practice Task

Write a program to take full the details of a student (Roll no, Student name, Class, Division) and display the details on the screen.

Challenge

Try to create your own mini project so that you will understand what you have truly learnt about C.