0% found this document useful (0 votes)
16 views14 pages

STRINGS

Strings in C are arrays of characters terminated by a null character, used for storing text. They can be declared and initialized in various ways, and functions from the <string.h> library are available for string manipulation, including copying, concatenating, and comparing strings. Input and output of strings can be managed using functions like scanf, printf, gets, and puts, with special considerations for handling whitespace.

Uploaded by

it10800223115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views14 pages

STRINGS

Strings in C are arrays of characters terminated by a null character, used for storing text. They can be declared and initialized in various ways, and functions from the <string.h> library are available for string manipulation, including copying, concatenating, and comparing strings. Input and output of strings can be managed using functions like scanf, printf, gets, and puts, with special considerations for handling whitespace.

Uploaded by

it10800223115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Strings

A String is an array of characters. Any group of characters defined


between double quotes is a constant string.
(Or) A string is a one-dimensional array of characters terminated by
a null character (‘\0’).

So, Strings are used for storing text/characters.

Eg: “INDIA”, “WELCOME”


Each character of the string occupies one-byte of memory. The
characters of the string are stored in contiguous memory locations.

Declaration and Initialization of Strings Syntax:

char string-name [size];


size determines number of characters in the string name. When the
compiler assigns a character string to a character array, it
automatically supplies a null character (‘\0’) at end of String.
Therefore, size should be equal to maximum number of character in
String plus one.
Eg:
char city [20];
Character arrays (strings) may be initializing when they are declared
(compile time initialization).
We can write--->>

 Initialize a character array as element by element and last


character should be the null character.
char city [10] = { ‘H’, ‘Y’, ‘D’, ‘E’, ‘R’, ‘A’, ‘B’, ‘A’, ‘D’, ‘\0’ } ;
 Initialize a character array as total string at a time.
char city [10] = “HYDERABAD”;
 Initialize a character array with out specifying the size, the
compiler automatically determines the size.
char city [ ] = “HYDERABAD”;
Using loop to print a character array:

int main( )
{
char name[ ] = "Santu Mondal" ;
int i = 0 ;
while ( name[i] != `\0' )
{
printf ( "%c", name[i] ) ;
i++ ;
}
return 0;
}

And here is the output...


Santu Mondal
Reading and Writing String:-

The input function scanf ( ) can be used with %s format specification


to read a string.
Eg:
char name[30];
scanf(“%s”, name);

The scanf() function to read multi word strings is doesn’t consider


white spaces. It terminates the input string at first occurrence of
white space. So multi word strings can’t read by using scanf()
function.
Eg:
char city[30];
scanf(“%s”,city);
If the input string is “NEW DELHI”, then the variable city can store
only “NEW”, it ignores the rest because white space is occurred after
NEW.
Example:
main( )
{
char name[25] ;
printf ( "Enter your name " ) ;
scanf ( "%s", name ) ;
printf ( "Hello %s!", name ) ;
}

And here is a sample run of the program...


Enter your name SANTU
Hello SANTU!

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:

To read multi word strings, we can use gets( ) function or getchar( )


function repeatedly.
Eg:
char city[20];
gets(city);
if the input string is “NEW DELHI”, then name contains total string
“NEW DELHI”.
(Or)

int i=0;
char city[20], ch;
do
{
ch=getchar(); name[ i ]=ch;
i++;
} while(ch != ‘ \n’)

Also print strings by using puts () function and putchar () function


repeatedly.
Eg:
puts(name)
OR
for(i=0;name[i]!=’\0’;i++)
{
putchar(name[i]);
}
Example:

PROGRAM TO READ A STRING AND PRINT IT


#include<stdio.h>
int main()
{
char ch, city[20];
int i;
printf("Enter a city name ");
i=0;
do
{
ch=getchar();
city[i]=ch;
i++;
}while(ch!='\n');
i=i-1;
city[i]='\0';
printf("The city is :");
puts(city);
return 0;
}
Ex2.
int main( )
{
char name[25] ;
printf ( "Enter your full name " ) ;
gets ( name ) ;
puts ( "Hello!" ) ;
puts ( name ) ;
} Output:
Enter your name Santu Mondal
Hello!
Santu Mondal
STRING HANDLING/MANIPULATION FUNCTIONS:

Operators can’t work with strings directly. So to manipulate strings


we have a large number of string handling functions in C standard
library and the responsible header file is <string.h>.
Some of those functions are:
strcpy ( ) ---->> Copies one String Over another,

strcmp ( ) ---->> Compares two Strings,

strcat ( ) ---->> Concatenates two Strings,

strlen ( ) ---->> Finds length of String,


strrev ( )

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);

String1 is appended with string2 by removing the null character of


string1 and string2 remains unchanged. The resultant string is stored
in string1.
Eg:
char city1[10] = “HELLO”;
char city2[12] = “WORLD”;
strcat (city1, city2);
Here city2 is appended to city1. So city1= “HELLOWORLD” and
city2=”WORLD”.

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”

Array of Pointers to Strings

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:

Original: raman srinivas


New: srinivas raman

You might also like