COMPUTER
PROGRAMMING
Strings
Topics to be covered
Reading, writing and manipulating
Strings
String definition and concepts
• Sequence of characters terminated by a null character (‘\
0’) is known as Strings.
• String Literal or String Constants:
– Sequence of characters enclosed within double quotes “”
– e.g. “A”, “ABC”, “ ”, “”, “Hello World”
• String Variable:
– 1-d array of characters terminated by a null (‘\0’).
– e.g. char a[10]
• All characters are converted into their corresponding
ASCII value and then stored in memory as contiguous
String Initialization
• char C[] = {'A','B','C','\0'}; // Character
array
• char S[] = "ABC";
• Both C and S have length = 3
• You can also have NULL instead of ‘\0’ defined
in stdio.h and string.h
String Initialization
1. Assigning a string literal without size
char str[] = "HelloWorld";
2. Assigning a string literal with a predefined size
• 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] = ”HelloWorld";
String Initialization
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] ={'H','e','l','l','o','W','o','r','l','d','\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[] = {'H','e','l','l','o','W','o','r','l','d','\0'};
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.
String Initialization
After declaration, if we want to assign some other
text to the string, we have to assign it one by one or
use built-in strcpy() function because the direct
assignment of string literal to character array is only
possible in declaration.
String input (ignoring blanks)
main()
{
char name[25] ;
printf ( "Enter name: " ) ;
scanf ( "%s", name ) ;
printf("hello %s",name);
}
• The C language does not provide an inbuilt data type for strings but it
has an access specifier “%s” which can be used to print and read strings
directly.
• We do not use the ‘&’ sign with the string name ‘str’ in scanf statement!
String input including blanks – Tough way
main()
{
char name[25] ;
printf ( "Enter name: " ) ;
scanf ( "%[^\n]s", name ) ; //This is called scanset
printf("hello %s",name);
}
Another way – Easy way
main(){
char name[20];
gets(name); // accepts blank spaces
//fgets(name, 20, stdin);
puts(name);
}
NULL and strlen()
main(){
int i=0;
char C[10];
gets(C);
while(C[i]!=NULL)
printf("%c",C[i++]);
//OR while(i<strlen(C)) printf("%c",C[i++]);
}
2-d character array
char Names[6][10] = {"akshay","parag", "raman"};
puts(Names[0]); //akshay
char Names[6][10] storage
Few Inbuilt functions
main( ) {
char A[] = "wxYZ", B[]="78", C[20]=“ABC”;
printf("length of %s = %d",A,strlen(A));
if(strcmp(A,B)==0)
printf("\nA,B same srings");
printf("\nC = %s",strcpy(C,A));
printf("\nConcat A+C = %s",strcat(C,A));
printf("\n Reverse A = %s",strrev(A));
}
strcmp() function
strcmp() function returns three different values after
the comparison of the two strings which are as
follows:
1. Zero ( 0 )
A value equal to zero when both strings are found to be
identical. That is, all of the characters in both strings are the
same.
2. Greater than Zero ( > 0 )
A value greater than zero is returned when the first not-
matching character in first_str has a greater ASCII value than
the corresponding character in second_str or we can also say
that if the character in first_str is lexicographically after the
character of second_str, then zero is returned.
strcmp() function
strcmp() function returns three different values
after the comparison of the two strings which are
as follows:
3. Lesser than Zero ( < 0 )
A value less than zero is returned when the first not-matching
character in first_str has a lesser ASCII value than the
corresponding character in second_str. We can also say that if
the character in first_str is lexicographically before the
character of second_str, zero is returned.
strcmp() function
int main()
{
// z has greater ASCII value than g
char first_str[] = "zfz";
char second_str[] = "gfg";
int res = strcmp(first_str, second_str);
if (res==0)
printf("Strings are equal");
else
printf("Strings are unequal");
printf("\nValue of result: %d" , res);
return 0;
}
strcmp() function
int main()
{
// declaring two same string
char first_str[] = "HelloWorld";
char second_str[] = "HelloWorld";
// printing the strings
printf("First String: %s\n", first_str);
printf("Second String: %s\n", second_str);
// printing the return value of the strcmp()
printf("Return value of strcmp(): %d", strcmp(first_str, second_str));
return 0;
}
strcat() function
The strcat() function will append a copy of the
source string to the end of destination string. The
strcat() function takes two arguments:
1) dest
2) src
It will append copy of the source string in the
destination string. The terminating character at
the end of dest is replaced by the first character
of src .
strncat() function
The strncat() function in C++ appends the given
number of character from one string to the end
of another string.The strncat() function will take
these three arguments:
1) Dest
2) Src
3) Count
This will append the given number of characters
from src string to the end of dest string. The
terminating character at the end of dest string
will be replaced by the first character of src
string .