1. R
ENGINEERING COLL
EGE
String in C
DEPARTMENT OF MASTER OF TECHNOLOGY (CSE)
5 YEARS
INTERGRATED
SUBJECT : PROBLEM SOLVING AND C
PROGRAMMING
BY:
MADHUMITHA N.S ES23CJ
22
MAHENDRAN B ES23CJ
23
MANIKANDAN R ES23CJ2
GUIDED BY :
MRS.R.SASIKALA
2. C O N T E N T S
2
o Introduction
o String Definition And Declaration
o Strings Methods
o Application In Strings
o Question Place
C programming:- Strings
3. INTRODUCTION
A string is a sequence of characters
It can contain letters, numbers, symbols and even
spaces
It represents within the double quotes (“ “).
It can be in two ways
Singular line string.
Multipleline string.
4. 4
C programming:- Strings
D E F I N I T I O N O F
S T R I N G :
o A String in C programming is a
sequence of characters
terminated with a null character
'0'.
o The C String is stored as an
array of characters
5. 5
C programming:- Strings
S T R I N G
D E C L A R AT I O N :
o Declaring a string in C is as simple as declaring a one-dimensional array.
o syntax for declaring a string.
char string_name[size];
o In the above syntax string_name is any name given to the string variable and size
is used to define the length of the string, i.e the number of characters strings will
store.
6. S T R I N G
I N I T I A L I Z AT I O N :
o Assigning a String Literal without Size
o Assigning a String Literal with a Predefined Size
o Assigning Character by Character with Size
o Assigning Character by Character without Size
6
C programming:- Strings
We can initialize a C string in 4 different ways which are as
follows
7. 1 . A S S I G N I N G A
S T R I N G L I T E R A L
W I T H O U T S I Z E
String literals can be assigned without
size. Here, the name of the string str
acts as a pointer because it is an array.
char str[] = "GeeksforGeeks";
C programming:- Strings
8. 2 . A S S I G N I N G A
S T R I N G L I T E R A L
W I T H A P R E D E F I N E D
S I Z E
String literals can be assigned with a
predefined size. But we should always
account for one extra space which will be
assigned to the null character. If we want
to store a string of size n then we should
always declare a string with a size equal to
or greater than n+1.
char str[50] = "GeeksforGeeks";
C programming:- Strings
9. 3 . A S S I N G N I N G
C H A R A C T E R B Y
C H A R A C T E R W I T H
S I Z E
We can also assign a string
character by character. But we
should remember to set the end
character as ‘0’ which is a null
character.
char str[14] = {
'G','e','e','k','s','f','o','r','G','e','e','k','s','
0'};
C programming:- Strings
10. 4 . A S S I G N I N G
C H A R A C T E R B Y
C H A R A C T E R
W I T H O U T S I Z E
We can assign character by character
without size with the NULL character at
the end. The size of the string is
determined by the compiler
automatically.
char str[] = {
'G','e','e','k','s','f','o','r','G','e','e','k','s','
0'};
C programming:- Strings
11. S T R I N G
M E T H O D S
STRING METHODS:
o strlen()
o strcpy()
o strcat()
o stcmp()
o strrev()
o strlwr()
o strupr()
* It, call string functions
* Header file<string.h> is
included.
C programming:- Strings
11
12. 12
C programming:- Strings
1.str len()
It returns the length of the string.
Eg:
#include <stdio.h>
#include <string.h>
int main() {
char a[10] = "welcome";
int l;
l = strlen(a);
printf (“String - %s",a);
printf(“Length - %d", l);
return 0;
}
Output :
String - Welcome
Length - 7.
13. 13
C programming:- Strings
2.str cpy()
Used to Copy the content from one string to another string
Eg:
#include<stdio.h>
#include<string.h>
int main(){
char a[]="hi";
char b[]="hello";
strcpy(a,b);
printf("a = %s",a);
printf("b =%s",b);
return 0;
}
Output :
a = hello
b= hello
14. 14
C programming:- Strings
3.str cat()
Joins two string
Eg:
#include<stdio.h>
#include<string.h>
int main(){
char a[]="hi";
char b[]="hello";
strcat(a,b);
printf("A = %s",a);
printf("B =%s",b);
return 0;
}
Output :
A = hi hello
B = hello
15. 15
C programming:- Strings
4.str cmp()
It will compare the two strings. If the two string are equal it return o. if the first string
greater than second string it return positive value. if the first string is shorter and the
second string is greater it return negative value.
Eg:
#include<stdio.h>
#include<string.h>
int main(){
char a[]="hi";
char b[]="hello";
int result;
result =strcmp(a,b);
printf(“The result=%s",result);
return 0;
} Output :
The result=-1
a>b -> positive num
a<b -> negative num
a==b -> zero
16. 16
C programming:- Strings
5.str r ev()
It is used to reverse the string
Eg:
#include <stdio.h>
#include <string.h>
int main() {
char a[10] = "welcome";
printf (" the reverse is %s",strrev(a));
return 0;
} Output :
the reverse is
emoclew
17. 17
C programming:- Strings
6.str lwr ()
It returns string characters in lowercase
Eg:
#include <stdio.h>
#include <string.h>
int main() {
char a[10] = "WELCOME";
printf (" The lower is %s",strlwr(a));
return 0;
}
Output :
The lower is
welcome
18. 18
C programming:- Strings
7.str upr ()
It returns string characters in uppercase
Eg:
#include <stdio.h>
#include <string.h>
int main()
{
char a[10] = "welcome";
printf (" The upper is %s",strupr(a));
return 0;
}
Output :
The upper is
WELCOME
19. A P P L I C AT I O
N O F
S T R I N G
C programming:- Strings
• Input/Output: Displaying and receiving text.
• Manipulation: Concatenation, copying, and
comparison operations.
• Searching : Finding substrings
• File Handling: Reading and writing strings to files.
• Command-Line Arguments: Handling arguments
passed to a C program.
19