STRINGS
STRINGS
int main( )
{
char name[ ] = "Santu Mondal" ;
int i = 0 ;
while ( name[i] != `\0' )
{
printf ( "%c", name[i] ) ;
i++ ;
}
return 0;
}
To output the string, you can use the printf() function together with
the format specifier %s to tell C that we are now working with strings:
e.g.
#include <stdio.h>
int main() {
char greetings[] = "Hello World!";
printf("%s", greetings);
return 0;
}
Access Strings:
Example:
#include <stdio.h>
int main() {
char greetings[] = "Hello World!";
printf("%c", greetings[0]);
return 0;
}
String Input/Output:
int i=0;
char city[20], ch;
do
{
ch=getchar(); name[ i ]=ch;
i++;
} while(ch != ‘ \n’)
String Length
It is to find out the length of the given string and it returns an integer
value, that is the number of characters in the given string.
Syntax:
strlen(string1);
It gives the length of string1.
Eg:
char city[10] = “HYDERABAD”;
strlen (city);
it gives the value 9.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("%d", strlen(alphabet));
Output:
return 0;
}
26
sizeof to get the size of a string/array.
Note that sizeof and strlen behaves differently, as sizeof also
includes the \0 character when counting.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("Length is: %d\n", strlen(alphabet));
printf("Size is: %d\n", sizeof(alphabet));
return 0;
}
OUTPUT:
Length is: 26
Size is: 27
sizeof will always return the memory size (in bytes), and not the
actual string length:
#include <stdio.h>
#include <string.h>
int main() {
char alphabet[50] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
printf("Length is: %d\n", strlen(alphabet));
printf("Size is: %d\n", sizeof(alphabet));
return 0;
}
OUTPUT:
Length is: 26
Size is: 50
Concatenate Strings
This function is used to join two strings together, and it returns the
resultant string.
Syntax:
strcat( string1,string2);
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Welcome ";
char str2[] = "Students!";
// Concatenate str2 to str1 (the result is stored in str1)
strcat(str1, str2);
// Print str1
printf("%s", str1);
return 0;
}
OUTPUT:
Welcome Students!
Copy Strings:
It is to copy one string into another, and it returns the resultant
string.
Syntax:
strcpy( string1,string2);
String2 is copied into string1.
Eg:
char city1[10] = “HYDERABAD”;
char city2[12] = “BANGLORE”;
strcpy (city1, city2);
Here city2 is copied into city1. So city1= “BANGLORE” and
city2=”BANGLORE”. The size of the array city1 should be large
enough to receive the contents of city2.
Example:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Hello AEC Students!";
char str2[20];
// Copy str1 to str2
strcpy(str2, str1);
// Print str2
printf("%s", str2);
return 0;
}
OUTPUT:
Hello AEC Students!
Compare Strings
It is to compare two strings to check their equality. If they are equal,
it returns zero, otherwise it returns the numeric difference between
the first non matching characters in the strings.
(i.e. +ve if first one is greater, -ve if first one is lesser).
Syntax:
strcmp (string1, string2);
Eg:
char city1[10] = “HYDERABAD”;
char city2[12] = “BANGLORE”;
strcpy (city1, city2);
Here city1 and city2 are compared, and returns the numeric
difference between ASCII value of ‘H’ and ASCII value of ‘B’ as they
are not equal.
Strcmp(“RAM”,ROM”); It returns some –ve value.
Strcmp(“RAM”,RAM”): It returns 0 as they are equal.
Example:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "Hello";
char str2[] = "Hello";
char str3[] = "Hi";
// Compare str1 and str2, and print the result
printf("%d\n", strcmp(str1, str2));
// Compare str1 and str3, and print the result
printf("%d\n", strcmp(str1, str3));
return 0;
}
OUTPUT:
0,
-4
strrev ( ):
This function is to find out the reverse of a given string.
Syntax:
strrev (string);
Eg:
char city[10] = “HYDERABAD”;
strrev (city);
it give the string “DABAREDYH”
char *names[ ] = {
"akshay",
"parag",
"raman",
"srinivas",
"gopal",
"rajesh"
};
In this declaration names[ ] is an array of pointers. It contains base
addresses of respective names. That is, base address of “akshay” is
stored in names[0], base address of “parag” is stored in names[1]
and so on.
Example:
int main( )
{
char *names[ ] = {
"akshay",
"parag",
"raman",
"srinivas",
"gopal",
"rajesh"
};
char *temp ;
printf ( "Original: %s %s", names[2], names[3] ) ;
temp = names[2] ;
names[2] = names[3] ;
names[3] = temp ;
printf ( "\nNew: %s %s", names[2], names[3] ) ;
}
OUTPUT: