SlideShare a Scribd company logo
STRING
A string in C is actually a character array. As an individual character variable can store
only one character, we need an array of characters to store strings. Thus, in C string is
stored in an array of characters. Each character in a string occupies one location in an
array. The null character ‘0’ is put after the last character. This is done so that program
can tell when the end of the string has been reached. The string in C programming
language is actually a one-dimensional array of characters which is terminated by a null
character '0'. Thus a null-terminated string contains the characters that comprise the
string followed by a null.
The following declaration and initialization create a string consisting of the word "Hello".
To hold the null character at the end of the array, the size of the character array
containing the string is one more than the number of characters in the word "Hello."
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'};

If you follow the rule of array initialization then you can write the above statement as
follows:
char greeting[] = "Hello";

Following is the memory presentation of above defined string in C.

Actually, you do not place the null character at the end of a string constant. The C
compiler automatically places the '0' at the end of the string when it initializes the array.
Let us try to print above mentioned string:
#include <stdio.h>
void main ()
{
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'};
printf("Greeting message: %sn", greeting );
}

getch();

In C programming, array of character are called strings. A string is terminated by null
character /0. For example:
"c string tutorial"

Here, "c string tutorial" is a string. When, compiler encounters strings, it appends null
character at the end of string.

Declaration of strings
Strings are declared in C in similar manner as arrays. Only difference is that, strings are
of char type.
char s[5];
Initialization of strings
In C, string can be initialized in different number of ways.
char c[]="abcd";
OR,
char c[5]="abcd";
OR,
char c[]={'a','b','c','d','0'};
OR;
char c[5]={'a','b','c','d','0'};

Reading words from user.
char c[20];
scanf("%s",c);

String variable c can only take a word. It is beacause when white space is encountered,
the scanf() function terminates.
Write a C program to illustrate how to read string from terminal.
#include <stdio.h>
void main(){
char name[20];
printf("Enter name: ");
scanf("%s",name);
printf("Your name is %s.",name);
getch();
}

Output
Enter name: sunil kumar
Your name is sunil.

Here, program will ignore kumar because, scanf() function takes only string before the
white space.
This process to take string is tedious. There are predefined functions gets() and puts in
C language to read and display string respectively.
#include <stdio.h>
void main(){
char name[30];
printf("Enter name: ");
gets(name);
//Function to read string from user.
printf("Name: ");
puts(name);
//Function to display string.
getch()
}

String handling functions
You can perform different type of string operations manually like: finding length of
string, concatenating(joining) two strings etc. But, for programmers ease, many library
function are defined under header file <string.h> to handle these commonly used talk
in C programming.

strlen()
In C, strlen() function calculates the length of string. It is defined under "string.h" header
file.
It takes only one argument, i.e, string name.
Syntax of strlen()
temp_variable = strlen(string_name);

Function strlen() returns the value of type integer.

Example of strlen()
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[10];
int len;
clrscr();
printf("Enter your String not more than 10 character::");
gets(name);
len=strlen(name);
printf("The Length of String is %d", len);
getch();
}
#include<stdio.h>
#include<conio.h>

#include <string.h>

void main()
{
char name[30]= "hello wass up";
clrscr();
printf("nString is %s",name);
printf("The length of string id %d",strlen(name));
getch();
}

strcpy()
Function strcpy() copies the content of one string to the content of another string. It is
defined under "string.h" header file.
It takes two arguments.

Syntax of strcpy()
strcpy(destination,source);

Here, source and destination are both the name of the string. This statement, copies the
content of string source to the content of string destination.

Example of strcpy()
#include <stdio.h>
#include <string.h>
void main(){
char a[10],b[10];
printf("Enter string: ");
gets(a);
strcpy(b,a);
//Content of string a is copied to string b.
printf("Copied string: ");
puts(b);
getch();
}
Output
Enter string: sunil kumar
Copied string: sunil kumar

strcat()
In C programming, strcat() concatenates(joins) two strings.
It takes two arguments, i.e, two strings and resultant string is stored in the first string
specified in the argument.
Function strcat() is defined under "string.h" header file.

Syntax of strcat()
strcat(first_string,second_string);

Example of strcat()
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[10],name1[10];
clrscr();
printf("Enter the First string");
gets(name);
printf("Enter the Second string");
gets(name1);
strcat(name,name1);
printf("The string after concatenations is %sn",name);
getch();
}

Example of strrev ()
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[10];
printf("Enter the String");
gets(name);
strrev(name);
printf("The String after reverse isn%s",name);
getch();
}

Example of strcmp ()
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char *str1 = "sample", *str2 = "sample";
clrscr();
if(strcmp(str1,str2)==0)
printf("strings are equal");
else
printf("strings are not equal");
getch();
}

More Related Content

What's hot (20)

PPTX
Functions in c
sunila tharagaturi
 
PPTX
Strings in Java
Abhilash Nair
 
PPS
String and string buffer
kamal kotecha
 
PPTX
Structures in c language
Tanmay Modi
 
PPTX
Functions in C
Kamal Acharya
 
PPTX
Loops in c programming
CHANDAN KUMAR
 
PPTX
Programming in c Arrays
janani thirupathi
 
PPTX
Templates in C++
Tech_MX
 
PPTX
Arrays In C++
Awais Alam
 
PPTX
Character set of c
Chandrapriya Rediex
 
PPTX
C Programming: Control Structure
Sokngim Sa
 
PPT
File handling in c
David Livingston J
 
PPTX
Unit 3. Input and Output
Ashim Lamichhane
 
PPTX
Looping statement in python
RaginiJain21
 
PPTX
Looping statements in C
Jeya Lakshmi
 
PPTX
Two dimensional arrays
Neeru Mittal
 
PPT
Basics of C programming
avikdhupar
 
PPTX
Functions in c++
Rokonuzzaman Rony
 
PPTX
Strings in C language
P M Patil
 
Functions in c
sunila tharagaturi
 
Strings in Java
Abhilash Nair
 
String and string buffer
kamal kotecha
 
Structures in c language
Tanmay Modi
 
Functions in C
Kamal Acharya
 
Loops in c programming
CHANDAN KUMAR
 
Programming in c Arrays
janani thirupathi
 
Templates in C++
Tech_MX
 
Arrays In C++
Awais Alam
 
Character set of c
Chandrapriya Rediex
 
C Programming: Control Structure
Sokngim Sa
 
File handling in c
David Livingston J
 
Unit 3. Input and Output
Ashim Lamichhane
 
Looping statement in python
RaginiJain21
 
Looping statements in C
Jeya Lakshmi
 
Two dimensional arrays
Neeru Mittal
 
Basics of C programming
avikdhupar
 
Functions in c++
Rokonuzzaman Rony
 
Strings in C language
P M Patil
 

Viewers also liked (16)

PPT
String c
thirumalaikumar3
 
PPTX
Applications of dielectric material
Grishma Rajput
 
PPTX
Dielectrics and its applications
Mohammed Limdiwala
 
PPT
DIELECTRICS PPT
Vaishnavi Bathina
 
PPT
Dielectric Material and properties
Mayank Pandey
 
PPTX
Function in c
Raj Tandukar
 
PPTX
Loops Basics
Mushiii
 
PPT
Structure in C
Fazle Rabbi Ador
 
PPTX
Arrays in C language
Shubham Sharma
 
PPT
Structure of a C program
David Livingston J
 
PPT
Structure c
thirumalaikumar3
 
PPTX
Array in c language
home
 
PPTX
Loops in C
Kamal Acharya
 
PPTX
Loops in C Programming
Himanshu Negi
 
PPTX
Function in C program
Nurul Zakiah Zamri Tan
 
Applications of dielectric material
Grishma Rajput
 
Dielectrics and its applications
Mohammed Limdiwala
 
DIELECTRICS PPT
Vaishnavi Bathina
 
Dielectric Material and properties
Mayank Pandey
 
Function in c
Raj Tandukar
 
Loops Basics
Mushiii
 
Structure in C
Fazle Rabbi Ador
 
Arrays in C language
Shubham Sharma
 
Structure of a C program
David Livingston J
 
Structure c
thirumalaikumar3
 
Array in c language
home
 
Loops in C
Kamal Acharya
 
Loops in C Programming
Himanshu Negi
 
Function in C program
Nurul Zakiah Zamri Tan
 
Ad

Similar to String in c (20)

DOCX
C Programming Strings.docx
8759000398
 
PPT
14 strings
Rohit Shrivastava
 
PPTX
Character array (strings)
sangrampatil81
 
PPT
Computer Programming- Lecture 5
Dr. Md. Shohel Sayeed
 
PPTX
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
AbhimanyuChaure
 
PPTX
String in programming language in c or c++
Azeemaj101
 
PDF
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
PPTX
introduction to strings in c programming
mikeymanjiro2090
 
PDF
String notes
Prasadu Peddi
 
PPTX
C Programming Language Part 11
Rumman Ansari
 
PPTX
Week6_P_String.pptx
OluwafolakeOjo
 
PPTX
COm1407: Character & Strings
Hemantha Kulathilake
 
PPTX
ARRAY's in C Programming Language PPTX.
MSridhar18
 
PDF
Unit 2
TPLatchoumi
 
PPTX
fundamentals of c programming_String.pptx
JStalinAsstProfessor
 
PPTX
Lecture 2. mte 407
rumanatasnim415
 
PDF
Principals of Programming in CModule -5.pdfModule-4.pdf
anilcsbs
 
PPTX
C Programming Unit-3
Vikram Nandini
 
PPTX
Lecture 15_Strings and Dynamic Memory Allocation.pptx
JawadTanvir
 
PPTX
Strings in programming tutorial.
Samsil Arefin
 
C Programming Strings.docx
8759000398
 
14 strings
Rohit Shrivastava
 
Character array (strings)
sangrampatil81
 
Computer Programming- Lecture 5
Dr. Md. Shohel Sayeed
 
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
AbhimanyuChaure
 
String in programming language in c or c++
Azeemaj101
 
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
introduction to strings in c programming
mikeymanjiro2090
 
String notes
Prasadu Peddi
 
C Programming Language Part 11
Rumman Ansari
 
Week6_P_String.pptx
OluwafolakeOjo
 
COm1407: Character & Strings
Hemantha Kulathilake
 
ARRAY's in C Programming Language PPTX.
MSridhar18
 
Unit 2
TPLatchoumi
 
fundamentals of c programming_String.pptx
JStalinAsstProfessor
 
Lecture 2. mte 407
rumanatasnim415
 
Principals of Programming in CModule -5.pdfModule-4.pdf
anilcsbs
 
C Programming Unit-3
Vikram Nandini
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
JawadTanvir
 
Strings in programming tutorial.
Samsil Arefin
 
Ad

More from Suneel Dogra (20)

PPT
Business model
Suneel Dogra
 
PDF
Internet
Suneel Dogra
 
PDF
Html
Suneel Dogra
 
PDF
Dreamweaver
Suneel Dogra
 
PDF
Advanced html
Suneel Dogra
 
PDF
Sql
Suneel Dogra
 
PDF
File organisation
Suneel Dogra
 
PDF
Distributed databases
Suneel Dogra
 
PDF
Database models
Suneel Dogra
 
PDF
Data base management system
Suneel Dogra
 
PPT
Web sitedesignpart1
Suneel Dogra
 
PPT
Web sitedesignpart1
Suneel Dogra
 
PPT
Internet security
Suneel Dogra
 
PDF
What is the linux
Suneel Dogra
 
DOC
He 12 different types of servers that every techie should know about
Suneel Dogra
 
PDF
Bachelor of computer application b.c.a.-2014
Suneel Dogra
 
DOC
Cloud computing application
Suneel Dogra
 
DOC
Fast track to linux
Suneel Dogra
 
DOC
A sorted linear array
Suneel Dogra
 
DOC
Jumping statements
Suneel Dogra
 
Business model
Suneel Dogra
 
Internet
Suneel Dogra
 
Dreamweaver
Suneel Dogra
 
Advanced html
Suneel Dogra
 
File organisation
Suneel Dogra
 
Distributed databases
Suneel Dogra
 
Database models
Suneel Dogra
 
Data base management system
Suneel Dogra
 
Web sitedesignpart1
Suneel Dogra
 
Web sitedesignpart1
Suneel Dogra
 
Internet security
Suneel Dogra
 
What is the linux
Suneel Dogra
 
He 12 different types of servers that every techie should know about
Suneel Dogra
 
Bachelor of computer application b.c.a.-2014
Suneel Dogra
 
Cloud computing application
Suneel Dogra
 
Fast track to linux
Suneel Dogra
 
A sorted linear array
Suneel Dogra
 
Jumping statements
Suneel Dogra
 

Recently uploaded (19)

PDF
cyanean_Lab_Reportdfd dfdsfsdfsd f dsf.pdf
REYESTACZAEDGARALEJA
 
PPTX
一比一原版(DCU毕业证书)都柏林城市大学毕业证如何办理
Taqyea
 
PPTX
一比一原版(Newman毕业证)纽曼大学毕业证如何办理
Taqyea
 
PPTX
一比一原版(UoD毕业证)邓迪大学毕业证如何办理
Taqyea
 
DOCX
The_Owl_and_the_Crow_Kalila_wa_Dimna.docx from fable
ranamutahir19
 
PPT
chapter 10.pptkkkkkkkkkkkkkkkkkkkkkkkkkk
trishalasharma7
 
PPTX
原版德国德累斯顿国际大学毕业证(DIU毕业证书)如何办理
Taqyea
 
PDF
Waddell Scavenger Hunt Presentation-2.pdf
peytonwaddell94
 
PDF
WKA #6: The Adventures - "FOUND FAMILY" TRANSCRIPT.pdf
Optimistic18
 
PPTX
Basic ppt templatecbnvjmbjbhknkl,vhv.pptx
Apurvanand Sahay
 
PPTX
Fun Friday for corporate virtual fridays
chandana94smiles
 
PDF
Sanchit batra best magician in India For all Events.pdf
Sanchit Batra
 
PPTX
HARASSMENT IN WORKPLACE FOR DEEP KNOWLEDGE
ashishmalikp2426
 
PPTX
Line Up Activity For Kids and Adult.....
cynthiamarasigan
 
PDF
Peyton Waddell Scavenger Hunt Presentation.pdf
peytonwaddell94
 
PPTX
OTT Channel Migration_AABC Overview_Operations Briefing.pptx
SobnavalleKhishmu
 
PPTX
Fun Friday. for corporate virtual fridays
chandana94smiles
 
PDF
How Modern Filmmakers Plan, Shoot, and Deliver Blockbuster Movies.pdf
All Writers Destination
 
PPTX
ARENA BOLA 1&2 Contigency Plan - ASTRO Broadcast pptx
SobnavalleKhishmu
 
cyanean_Lab_Reportdfd dfdsfsdfsd f dsf.pdf
REYESTACZAEDGARALEJA
 
一比一原版(DCU毕业证书)都柏林城市大学毕业证如何办理
Taqyea
 
一比一原版(Newman毕业证)纽曼大学毕业证如何办理
Taqyea
 
一比一原版(UoD毕业证)邓迪大学毕业证如何办理
Taqyea
 
The_Owl_and_the_Crow_Kalila_wa_Dimna.docx from fable
ranamutahir19
 
chapter 10.pptkkkkkkkkkkkkkkkkkkkkkkkkkk
trishalasharma7
 
原版德国德累斯顿国际大学毕业证(DIU毕业证书)如何办理
Taqyea
 
Waddell Scavenger Hunt Presentation-2.pdf
peytonwaddell94
 
WKA #6: The Adventures - "FOUND FAMILY" TRANSCRIPT.pdf
Optimistic18
 
Basic ppt templatecbnvjmbjbhknkl,vhv.pptx
Apurvanand Sahay
 
Fun Friday for corporate virtual fridays
chandana94smiles
 
Sanchit batra best magician in India For all Events.pdf
Sanchit Batra
 
HARASSMENT IN WORKPLACE FOR DEEP KNOWLEDGE
ashishmalikp2426
 
Line Up Activity For Kids and Adult.....
cynthiamarasigan
 
Peyton Waddell Scavenger Hunt Presentation.pdf
peytonwaddell94
 
OTT Channel Migration_AABC Overview_Operations Briefing.pptx
SobnavalleKhishmu
 
Fun Friday. for corporate virtual fridays
chandana94smiles
 
How Modern Filmmakers Plan, Shoot, and Deliver Blockbuster Movies.pdf
All Writers Destination
 
ARENA BOLA 1&2 Contigency Plan - ASTRO Broadcast pptx
SobnavalleKhishmu
 

String in c

  • 1. STRING A string in C is actually a character array. As an individual character variable can store only one character, we need an array of characters to store strings. Thus, in C string is stored in an array of characters. Each character in a string occupies one location in an array. The null character ‘0’ is put after the last character. This is done so that program can tell when the end of the string has been reached. The string in C programming language is actually a one-dimensional array of characters which is terminated by a null character '0'. Thus a null-terminated string contains the characters that comprise the string followed by a null. The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello." char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'}; If you follow the rule of array initialization then you can write the above statement as follows: char greeting[] = "Hello"; Following is the memory presentation of above defined string in C. Actually, you do not place the null character at the end of a string constant. The C compiler automatically places the '0' at the end of the string when it initializes the array. Let us try to print above mentioned string: #include <stdio.h> void main () { char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'}; printf("Greeting message: %sn", greeting ); } getch(); In C programming, array of character are called strings. A string is terminated by null character /0. For example: "c string tutorial" Here, "c string tutorial" is a string. When, compiler encounters strings, it appends null character at the end of string. Declaration of strings Strings are declared in C in similar manner as arrays. Only difference is that, strings are of char type. char s[5];
  • 2. Initialization of strings In C, string can be initialized in different number of ways. char c[]="abcd"; OR, char c[5]="abcd"; OR, char c[]={'a','b','c','d','0'}; OR; char c[5]={'a','b','c','d','0'}; Reading words from user. char c[20]; scanf("%s",c); String variable c can only take a word. It is beacause when white space is encountered, the scanf() function terminates. Write a C program to illustrate how to read string from terminal. #include <stdio.h> void main(){ char name[20]; printf("Enter name: "); scanf("%s",name); printf("Your name is %s.",name); getch(); } Output Enter name: sunil kumar Your name is sunil. Here, program will ignore kumar because, scanf() function takes only string before the white space. This process to take string is tedious. There are predefined functions gets() and puts in C language to read and display string respectively. #include <stdio.h> void main(){ char name[30]; printf("Enter name: "); gets(name); //Function to read string from user. printf("Name: "); puts(name); //Function to display string. getch() } String handling functions You can perform different type of string operations manually like: finding length of string, concatenating(joining) two strings etc. But, for programmers ease, many library function are defined under header file <string.h> to handle these commonly used talk in C programming. strlen() In C, strlen() function calculates the length of string. It is defined under "string.h" header file. It takes only one argument, i.e, string name.
  • 3. Syntax of strlen() temp_variable = strlen(string_name); Function strlen() returns the value of type integer. Example of strlen() #include<stdio.h> #include<conio.h> #include<string.h> void main() { char name[10]; int len; clrscr(); printf("Enter your String not more than 10 character::"); gets(name); len=strlen(name); printf("The Length of String is %d", len); getch(); } #include<stdio.h> #include<conio.h> #include <string.h> void main() { char name[30]= "hello wass up"; clrscr(); printf("nString is %s",name); printf("The length of string id %d",strlen(name)); getch(); } strcpy() Function strcpy() copies the content of one string to the content of another string. It is defined under "string.h" header file. It takes two arguments. Syntax of strcpy() strcpy(destination,source); Here, source and destination are both the name of the string. This statement, copies the content of string source to the content of string destination. Example of strcpy() #include <stdio.h> #include <string.h> void main(){ char a[10],b[10]; printf("Enter string: "); gets(a); strcpy(b,a); //Content of string a is copied to string b. printf("Copied string: "); puts(b); getch(); }
  • 4. Output Enter string: sunil kumar Copied string: sunil kumar strcat() In C programming, strcat() concatenates(joins) two strings. It takes two arguments, i.e, two strings and resultant string is stored in the first string specified in the argument. Function strcat() is defined under "string.h" header file. Syntax of strcat() strcat(first_string,second_string); Example of strcat() #include<stdio.h> #include<conio.h> #include<string.h> void main() { char name[10],name1[10]; clrscr(); printf("Enter the First string"); gets(name); printf("Enter the Second string"); gets(name1); strcat(name,name1); printf("The string after concatenations is %sn",name); getch(); } Example of strrev () #include<stdio.h> #include<conio.h> #include<string.h> void main() { char name[10]; printf("Enter the String"); gets(name); strrev(name); printf("The String after reverse isn%s",name); getch(); } Example of strcmp () #include <stdio.h> #include <string.h> #include<conio.h> void main() { char *str1 = "sample", *str2 = "sample"; clrscr(); if(strcmp(str1,str2)==0) printf("strings are equal"); else printf("strings are not equal"); getch(); }