0% found this document useful (0 votes)
48 views

String in C

String is the char type array in which elements are various characters. Each character is a an individual element. every element has an Unique index number. With the help of Index number ,You can access elements of strings Character arrays has a '\0' which is a null character at the end as a last element of the character array. Even if you have not specified the '\0' character at the end but the compiler will automatically place the '\0' character at the end. There is no need of specifying the

Uploaded by

ayan shaikh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

String in C

String is the char type array in which elements are various characters. Each character is a an individual element. every element has an Unique index number. With the help of Index number ,You can access elements of strings Character arrays has a '\0' which is a null character at the end as a last element of the character array. Even if you have not specified the '\0' character at the end but the compiler will automatically place the '\0' character at the end. There is no need of specifying the

Uploaded by

ayan shaikh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

6/29/2019 String In C | Programmerdouts

Home C Language  Practice programs

String In C | Programmerdouts
By Programmerdouts - June 06, 2019

      Mos

String

String is the char type array in which elements are various


characters.
Each character is a an individual element.
every element has an Unique index number.
With the help of Index number ,You can access elements of strings
Character arrays has a '\0' which is a null character at the end as
a last element of the character array.
Even if you have not specified the '\0' character at the end but the Follo
compiler will automatically place the '\0' character at the end. Get al
There is no need of specifying the '\0' null character at the end to you
,while initializing the character array ,or while giving the input as string,
Emai
because Compiler automatically assign '\0' character at the end

Declaration of String

Cate
Syntax:

datatype name[size_of_char_array];

About

Let's declare some character array.

https://programmerdouts.blogspot.com/2019/06/string-in-c.html 1/9
6/29/2019 String In C | Programmerdouts

#include<stdio.h>

void main()
{
char char1[20]; //this char array can store 20 charac

char char2[10]; //this char array can store 10 chara


Powe
}

C lan
Initialization of character array.
Basic

Initializing of character array can done in multiple ways.


Arrays
Let's see them.
Strings

#include<stdio.h>

void main()
{
char char1[5] = {'a','b','c','d','e'}; //One of th

//another way of initialization, and this is a be


//whole char array is initialized under the double
Ad S
char char2[5] = "abcde"; //each character is a
//each element has inde
//here index of characters are a=0,b=1,c=2,d=3,e=4.

//another way of initializing char array is without sp Det

char char3[] = "abcde";

char char4[] = {'a','b','c','d','e'};//without spe


}

Below image will give the view of how strings are stored in memory.

https://programmerdouts.blogspot.com/2019/06/string-in-c.html 2/9
6/29/2019 String In C | Programmerdouts

Bloc
Basic
INR 6

Cor
conce

Accessing the elements.


Join O
Dapat
Langs
Your e
As you know every element in character array has an unique index
number.
With the help of index number we will access the elements.
let access some elements of character array. Array
Basics
Contr
Funct
#include<stdio.h> Input
Pointe
Practi
void main() String
{ char first_char1,second_char1; Struct
char first_char2,second_char2;
char char1[5] = {'a','b','c','d','e'};

//Accessing elements of char1 array.


first_char1 = char1[0]; //assigning first elemen
second_char1 = char1[1]; //assigning second eleme

printf("\nFirst element of char1 array is %c and s

char char2[10] = "Helloworld";

https://programmerdouts.blogspot.com/2019/06/string-in-c.html 3/9
6/29/2019 String In C | Programmerdouts

//Accessing elements of char1 array.


first_char2 = char2[0]; //assigning first eleme
second_char2 = char2[1]; //assigning second eleme

printf("\nFirst element of char2 array is %c and s

Output:

First element of char1 array is a and second element is b.


First element of char2 array is H and second element is e.

How to take string as input?


C LA
taking string as input can done with scanf() function
You can take string as input and print on the screen by using
specifier %s
lets take some input and print them on screen.

Syntax: C Pr
Abso
INR

S
scanf("%s",var); // taking string as input.
//for taking string as input there is no need of
printf("%s",var); //printing the string.

lets see simple program.

#include<stdio.h>

void main()
https://programmerdouts.blogspot.com/2019/06/string-in-c.html 4/9
6/29/2019 String In C | Programmerdouts

{
char s[10];

printf("Enter any string:");


scanf("%s",s);

printf("\nstring is %s",s);

Output:

Enter any string: Programmer


string is Programmer

But ,scanf() and printf() have a one drawback.


This function can't accept or print the string containing space.
this function take space as deliminator
Let's understand it by program

#include<stdio.h>

void main()
{
char s[10];

printf("Enter any string:");


scanf("%s",s);

printf("\nstring is %s",s);

Output:

Enter any string:Programmers are great


string is Programmers

This is how printf() and scanf() does not work on string containing spaces.

https://programmerdouts.blogspot.com/2019/06/string-in-c.html 5/9
6/29/2019 String In C | Programmerdouts

To overcome from this drawback ,'C' language provides built-in


functions
gets() and puts() You can use this function while working with string
containing spaces.
gets() & puts()

Further Concepts:

Strings In C

What are Strings

Concept of strings

Input And Output Functions

How to take strings As Input

What is gets() functions

What is puts() functions

2D Strings

Concept of 2D Strings

What is Character array

what are various string handling functions

Functions for Characters

Functions from #include<string.h> Header File

what are strcpy(), strcmp(), strncmp(), strcmpi(),etc

strcmp() VS strncmp() VS strcmpi()

Functions from #include<ctype.h> Header File

what are isdigit(), islower() , isupper(),etc

Various Practice Programs on strings

https://programmerdouts.blogspot.com/2019/06/string-in-c.html 6/9

You might also like