UNIT-1 String K
UNIT-1 String K
• String in C
• Intorduction ,Declaration,Initialization,
• String I/O ,Array of string
• String Manupulation Function :-
• String Length,Copy,Compare Concatenate
search for sub string
Arrays
Strings
▪ A string is a series of characters treated as a single unit.
▪ A string may include letters, digits and various special characters
such as +,-,*,/ and $.
▪ String literals or string constants are written in double quotation
marks.
For example : - “LOTUS” “DH0019” “12345” “Welcome to Malaysia”
▪ A string in C is an Array of characters ending with the “null”
character(‘\0’).
▪ C has no type “String”
▪ A string constant is a list of characters enclosed in double quotes.
For example,
char str1[ 12]=“hello there”;
▪ Character arrays also can be initialized with individual character
constants in an initializer list.
For example,
char s1[ ]={‘f’,’i’,’r’,’s’,’t’,’\0’};
Arrays Slide 6 (of 38)
Declaration of string
Arrays
Initializing a String
A string can be initialized in different ways. We will explain this with the help
of an example..
Arrays
3. Assigning character by character with size: 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'};
4. Assigning character by character without size: 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'};
Below is the memory representation of the string “Geeks”.
Arrays
Strings I/O
For example,
char color[ ]=“White”;
or.
char color[5];
scanf(“%s”,color);
printf(“%s”,color);
“%s” is a control string for the strings.
Arrays
Character Arrays 2/3
printf(“String 1 is:“);
for(i=0; s1[i] !=‘\0’;i++)
printf(“%c”,s1[i]);
printf(“\n”);
printf(“String 1 with spaces between characters:\n“);
for(i=0;s1[i] !=‘\0’;i++)
This is how you tell if
printf(“%c ”,s1[i]); you are at the end of
printf(“\n”); the string.
printf(“String 2 is:”);
for(i=0;s2[i] !=‘\0’;i++)
printf(“%c”,s2[i]);
printf(“\n”);
}
Arrays
Standard Library String Functions
strlen( )
• This function counts the number of characters present
in a string.
main( )
{
char arr[ ] = "Bamboozled" ;
int len1, len2 ;
len1 = strlen ( arr ) ;
len2 = strlen ( "Humpty Dumpty" ) ;
printf ( "\nstring = %s length = %d", arr, len1 ) ;
printf ( "\nstring = %s length = %d", "Humpty Dumpty", len2
);
}
Arrays Slide 8 (of 38)
Standard Library String Functions
The output would be...
string = Bamboozled length = 10
string = Humpty Dumpty length = 13
• Note that in the f irst call to the function strlen( ), the
base address of the string is passed , and the function in
turn returns the length of the string.
While calculating the length it doesn’t count ‘\0’.
Even in the second call, len2 = strlen ( "Humpty Dumpty" ) ;
what gets passed to strlen( ) is the address of the string
and not the string itself
Arrays
#include <stdio.h>
#include <string.h>
int main(void)
{
//variable
char str1[100], str2[100];
//input
printf("Enter string 1: ");
gets(str1);
printf("Enter string 2: ");
gets(str2);
if (strcmp(str1, str2) == 0) {
printf("Both are same.\n");
}
else {
printf("Both are different.\n");
}
printf("End of code\n");
return 0;
}
Arrays
strcat() function
The strcat() is used to concatenate two strings. The second
string will be appended to the end of the first string. This
process is called concatenation.
Syntax strcat (StringVariable1, StringVariable 2);
Arrays
#include <stdio.h>
#include <string.h>
int main(void)
{
//variable
char
str1[] = "Hello",
str2[] = "World",
str3[100] = "";
//concat
strcat(str3, str1); //concat "Hello" to str3 so, str3 = "Hello"
strcat(str3, " "); //concat " " to str3 so, str3 = "Hello "
strcat(str3, str2); //concat "World" to str3 so, str3 = "Hello World"
printf("End of code\n");
return 0;
Arrays
strcpy (Destination string,source string):
Arrays
#include <stdio.h>
#include <string.h>
int main(void)
{
//variable
char
str1[] = "Hello",
str2[] = "World",
return 0;
Arrays
Sub String
Arrays
#include <string.h>
#include <stdio.h>
int main()
{
// Take any two strings
char s1[] = "GeeksforGeeks";
char s2[] = "for";
char* p;
// Find first occurrence of s2 in s1
p = strstr(s1, s2);
Arrays
// Prints the result
if (p) {
printf("String found\n");
printf("First occurrence of string '%s' in '%s'
is '%s'", s2, s1, p);
} else
printf("String not found\n");
return 0;
}
Arrays