2. String
• The C String is stored as an array of characters.
• A String in C programming is a sequence of
characters terminated with a null character
‘0’.
• The difference between a character array and
a C string is the string is terminated with a
unique character ‘0’.
3. String Declaration
• A string in C is declared as a one-dimensional
array.
– Syntax
• char string_name[size];
– 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.
Char str[ ];
4. Memory Representation of String
Char str[ ]=“Sethu”
Index
Varaiable
Address
There is an extra terminating character which is the Null character (‘0’) used to
indicate the termination of a string that differs strings from normal character
arrays.
S e t h u
0 1 2 3 4 5
0
0x 1x 2x 3x 4x 5x
We do not place the null
characters at the end of the
string Constant.
The C Compiler automatically
places the’0’ at the end of the
string when it initializes the
array.
5. C String Initialization
4 different ways
Method1
1. Assigning a string literal without size
char str[] = “SethuInstituteofTechnology";
String literals can be assigned without size.
Here, the name of the string str acts as a pointer
because it is an array.
A literal is a token that denotes a fixed value, which may be an integer , a floating-point
number, a character, or a string.
6. C String Initialization
Method-2
• 2. Assigning a string literal with a predefined size
char str[50] = “InformationTechnology";
String literals can be assigned with a predefined size.
One extra space which will be assigned to the null character.
If you want to store a string of size n then youshould always
declare a string with a size equal to or greater than n+1.
7. C String Initialization
Method-3
• 3. Assigning character by character with size
Assign a string character by character.
char str[15] = {
‘S','e',‘t',‘h',‘u',‘I',‘n',‘s',‘t',‘i',‘t',‘u','t',‘e','0'};
But you should remember to set the end
character as ‘0’ which is a null character.
8. C String Initialization
Method-4
• Assigning character by character without size
• Assign character by character without size
with the NULL character at the end.
• char str[] =
{‘S','e',‘t',‘h',‘u',‘I',‘n',‘s',‘t',‘i',‘t',‘u','t',‘e','0'};
• The size of the string is determined by the
compiler automatically.
9. Note
• When a Sequence of characters enclosed in
the double quotation marks is encountered by
the compiler, a null character ‘0’ is appended
at the end of the string by default.
12. String Function
• String Length
To get the length of a string,
strlen()
Ex
char alphabet[]
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("%d", strlen(alphabet));
Output
26
13. String Function
• sizeof to get the size of a string/array
• char alphabet[]
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("%d", strlen(alphabet)); // 26
printf("%d", sizeof(alphabet)); // 27
• sizeof will always return the memory size
(in bytes)
• sizeof also includes the 0 character when
counting
14. String Function
• Concatenate Strings
– To concatenate (combine) two strings,
strcat()
Ex
char str1[20] = "Hello ";
char str2[] = "World!";
// Concatenate str2 to str1 (result is stored in str1)
strcat(str1, str2);
// Print str1
printf("%s", str1);
Output
• Hello World!
15. String Function
• Copy Strings
– To copy the value of one string to another,
• strcpy()
• Ex
• char str1[20] = "Hello World!";
char str2[20];
// Copy str1 to str2
strcpy(str2, str1);
// Print str2
printf("%s", str2);
• Output
• Hello World!
16. Compare Strings
• To compare two strings, you can use the strcmp() function.
• It returns 0 if the two strings are equal, otherwise a value that is not 0:
• Ex
• char str1[] = "Hello";
char str2[] = "Hello";
char str3[] = "Hi";
// Compare str1 and str2, and print the result
printf("%dn", strcmp(str1, str2)); // Returns 0 (the strings are equal)
// Compare str1 and str3, and print the result
printf("%dn", strcmp(str1, str3)); // Returns -4 (the strings are not equal)
• Output
• 0
-4
17. Program-1 Find the Length of the
String using Strlen() Function
// C program to illustrate strings
#include <stdio.h>
#include <string.h>
int main()
{
// declare and initialize string
char str[] = “Priya";
// print string
printf("%sn", str);
int length = 0;
length = strlen(str);
// displaying the length of string
printf("Length of string str is %d", length);
return 0;
}
18. Program -2 Read String from the user
and print the Same Using %s Access
Specifier
// C program to read string from user
#include<stdio.h>
int main()
{
// declaring string
char str[50];
// reading string
scanf("%s",str);
// print string
printf("%s",str);
return 0;
}
19. String Array
• In C programming String is a 1-D array of
characters and is defined as an array of
characters.
• But an array of strings in C is a two-
dimensional array of character types.
• Each String is terminated with a null character
(0). It is an application of a 2d array.
20. Syntax:
char variable_name[r] = {list of string};
• Here,
• var_name is the name of the variable in C.
• r is the maximum number of string values that
can be stored in a string array.
• c is the maximum number of character values
that can be stored in each string array.
21. // C Program to print Array of strings
#include <stdio.h>
int main()
{
char arr[3][13] = {"C Program",
"Python", "Java Program"};
printf("String array Elements are:n");
for (int i = 0; i < 3; i++)
{
printf("%sn", arr[i]);
}
return 0;
}
Output
String array Elements are:
C Program
Python
Java Program
22. Memory Representation
C P r o g r a m 0
P y t h o n 0
J a v a p r o g r a m 0
0 1 2 3 4 5 6 7 8 9 10 11 12
Index
Array [0]
Array [1]
Array [2]
the size of the array of strings the space consumption is high
24. Program
#include <stdio.h>
#include <string.h>
// function definition of the revstr()
void revstr(char *str1)
{
// declare variable
int i, len, temp;
len = strlen(str1); // use strlen() to get the
length of str string
// use for loop to iterate the string
for (i = 0; i < len/2; i++)
{
// temp variable use to temporary hold
the string
temp = str1[i];
str1[i] = str1[len - i - 1];
str1[len - i - 1] = temp;
}
}
int main()
{
char str[50]; // size of char string
printf (" Enter the string: ");
gets(str); // use gets() function to take string
printf (" n Before reversing the string: %s
n", str);
// call revstr() function
revstr(str);
printf (" After reversing the string: %s", str);
}
Output
Enter the string: sethu institute
Before reversing the string: sethu institute
After reversing the string: etutitsni uhtes