LectureMaterial 1
LectureMaterial 1
C String Initialization
#include <stdio.h>
#include<stdio.h>
#include <string.h>
int main()
printf("%s\n", str);
int length = 0;
length = strlen(str);
return 0;
}
Output:
Geeks
Length of string str is 5
Size of string is 6
Example 2:
Read a String Input From the User
#include<stdio.h>
int main()
{
// declaring string
char str[50];
// reading string
scanf("%s", str);
// print string
printf("%s",str);
return 0;
}
Input
GeeksforGeeks
Output
GeeksforGeeks
We can see in the above program that the string can also be read using a single scanf
statement. Also, we might be thinking that why we have not used the ‘&’ sign with the
string name ‘str’ in scanf statement!
We know that the ‘&’ sign is used to provide the address of the variable to the scanf()
function to store the value read in memory. As str[] is a character array so using str
without braces ‘[‘ and ‘]’ will give the base address of this string. That’s why we have
not used ‘&’ in this case as we are already providing the base address of the string to
scanf.
Example 3:
// C Program to take input string which is separated by whitespaces
#include <stdio.h>
int main()
{
char str[20];
scanf("%s", str);
printf("%s", str);
return 0;
}
Input
Geeks for Geeks
Output
Geeks
Here, the string is read only till the whitespace is encountered.
Example 4:
// C program to illustrate fgets()
#include <stdio.h>
#define MAX 50
int main()
{
char str[MAX];
return 0;
}
Input
Geeks for Geeks
Output
String is:
Geeks for Geeks
Example 5:
// C Program to take string separated by whitespace using scanset characters
#include <stdio.h>
int main()
{
char str[20];
// using scanset in scanf
scanf("%[^\n]s", str);
// printing read string
printf("%s", str);
return 0;
}
Input
Geeks for Geeks
Output
Geeks for Geeks
String Handling Functions:
strcpy() strcpy(string1, string2) Copies string2 value into string1
Strlen()
The strlen() function calculates the length of a given string.
The strlen() function takes a string as an argument and returns its length. The
returned value is of type size_t (an unsigned integer type).
Example
#include
<stdio.h>
#include
<string.h> int
main()
{
char a[20]="Program";
char b[20]={'P','r','o','g','r','a','m','\0'}; // using the %zu format specifier to
print size_t
printf("Length of string a = %zu \n",strlen(a));
printf("Length of string b = %zu
\n",strlen(b)); return 0;
}
Output
Length of string a = 7
Length of string b = 7
Note: strlen() function doesn't count the null character \0 while calculating the length
Strcpy()
The strcpy() function copies the string pointed by source (including the null character)
to the destination. The strcpy() function also returns the copied string.
The syntax is
char* strcpy(char* destination, const char* source);
Example
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "C programming";
char str2[20]; // copying str1 to str2
strcpy(str2, str1);
puts(str2); // C programming
return 0;
}
Output
C programming
Note: In strcpy(), the size of the destination string should be large enough to store the
copied string. Otherwise, it may result in undefined behavior.
Strcmp()
The strcmp() compares two strings character by character. If the strings
are equal, the function returns 0.
Output
strcmp(str1, str2) = 1
strcmp(str1, str3) = 0
Strcat()
In C programming, the strcat() function concatenates (joins) two strings.
The function definition of strcat() is:
char *strcat(char *destination, const char *source)
Example
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "This is ",
str2[] = "programiz.com";
// concatenates str1 and str2 the resultant string is stored in str1.
strcat(str1, str2);
puts(str1);
puts(str2);
return 0;
}
Output
This is programiz.com
programiz.com
Note:
The strcat() function concatenates the destination string and the source
string, and the result is stored in the destination string.
The size of the destination string should be large enough to store the
resultant string. If not, we will get the segmentation fault error.
Syntax:-
char string-array-name[row-size][column-size];
char language[5][10] =
{"Java", "Python", "C++", "HTML", "SQL"};
char largestcity[6][15] =
{"Tokyo", "Delhi", "Shanghai", "Mumbai", "Beijing", "Dhaka"};
char language[5][10] =
{
{'J','a','v','a','\0'},
{'P','y','t','h','o','n','\0'},
{'C','+','+','\0'},
{'H','T','M','L','\0'},
{'S','Q','L','\0'}
};
Each String in this array can be accessed by using its index number.
The index of the array always starts with 0.
language[0] => "Java";
language[1] => "Python";
language[2] => "C++";
language[3] => "HTML";
language[4] => "SQL";
// it is valid
char language[ ][10] = {"Java", "Python", "C++", "HTML", "SQL"};
// invalid
char language[5][ ] = {"Java", "Python", "C++", "HTML", "SQL"};
Or,
Or,
#include<stdio.h>
int main()
{
// declaring and initializing 2D String
char language[5][10] = {"Java", "Python", "C++", "HTML",
"SQL"};
// Dispaying strings
printf("Languages are:\n");
for(int i=0;i<5;i++)
puts(language[i]);
return 0;
}
Output:-
Languages are:
Java
Python
C++
HTML
SQL