Array & String
Array & String
Array & String
Array of 10 elements
Array
• Advantage of array
• Code Optimization: Less code is required, one variable can store
numbers of value.
• Easy to traverse data: By using array easily retrieve the data of array.
• Easy to sort data: Easily short the data using swapping technique
• Random Access: With the help of array index you can randomly access
any elements from array.
• Dis-Advantage of array
• Fixed Size: Whatever size, we define at the time of declaration of array,
we can not change their size, if you need more memory in that time you
can not increase memory size, and if you need less memory in that case
also wastage of memory.
Declaring Array
• To declare an array in C you need to declare datatype and size of
an array.
• Syntax
• datatype arrayname[SIZE];
• Example
• int rollno[10];
Initializing Array
• Initializing is a process to initialize the value in array variable. This is happen
in two ways, initialize array one by one or all elements are initializing once.
1) Initialization of array one by one
int arr[5];
arr[0]=10;
arr[1]=20;
arr[2]=30;
arr[3]=40;
arr[4]=50;
2) Initialization of array at once
int arr[]={10,20,30,40,50};
Accessing Array Elements
• Example
char str[5];
scanf(“ %s",str); //no & in string
Example
#include<stdio.h>
#include<conio.h>
Note: String variable can only take
void main() only one word. It is because when
{ white space is encountered, the
scanf() function terminates. to over
char name[10];
come this problem you can use gets()
printf("Enter name: "); function.
scanf("%s",name);
printf("Your name is: %s.",name);
getch();
}
gets() & puts()
• gets()
• gets() are used to get input as a string from keyword, using gets() we can
input more than one word at a time.
• Syntax
char str[5];
gets(str);
• puts()
• puts() are used to print output on screen, generally puts() function are
used with gets() function.
• Syntax
puts(str);
Example of String program
gets() & puts()
#include<stdio.h>
#include<conio.h>
void main()
{
char str[10];
printf("Enter any string: ");
gets(str);
printf("String are: ");
puts(str);
getch();
}
C Library String functions
(Built in String Functions)
• All the library function of String is available in string.h header file.
No Function Purpose
#include <stdio.h>
#include<conio.h>
#include <string.h>
Void main()
{
char s1[30] = "string 1";
char s2[30] = "string 2 : I’m gonna copied into s1";
/* this function has copied s2 into s1*/
strcpy(s1,s2);
Output:
printf("String s1 is: %s", s1); String s1 is: string 2: I’m gonna copied into s1
getch();
strcat(s1,s2);
It concatenates two strings and returns the concatenated string.
#include <stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
char s1[10] = "Hello";
char s2[10] = "World";
strcat(s1,s2);
printf("Output string after concatenation: %s", s1);
getch(); Output:
Output string after concatenation: HelloWorld
}
strncat(s1,s2,n);
It concatenates n characters of str2 to string str1. A terminator char (‘.\ 0 .’) will always be appended
at the end of the concatenated string.\
#include <stdio.h>
#include<conio.h>
#include <string.h>
int main()
{
char s1[10] = "Hello";
char s2[10] = "World";
strncat(s1,s2, 3);
printf("Concatenation using strncat: %s", s1);
getch(); Output:
Concatenation using strncat: HelloWor
}
strlen(s1);
It returns the length of the string without including end character (terminating char ‘\0’).
#include <stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
char str1[20] = "BeginnersBook";
printf("Length of string str1: %d", strlen(str1)); Output:
Length of string str1: 13
getch();
} strlen vs sizeof
strlen(str1) returned value 13.
sizeof(str1) would return value 20 as the array size is 20 (see the first statement in main function).
strnlen(s1,n);
It returns length of the string if it is less than the value specified for maxlen (maximum
length) otherwise it returns maxlen value.
#include <stdio.h>
#include<conio.h>
#include <string.h>
int main()
{
char str1[20] = "BeginnersBook";
printf("Length of string str1 when maxlen is 30: %d", strnlen(str1, 30));
printf("Length of string str1 when maxlen is 10: %d", strnlen(str1, 10));
getch(); Output:
Length of string str1 when maxlen is 30: 13
} Length of string str1 when maxlen is 10: 10
strcmp(s1,s2);
It compares the two strings and returns an integer value. If both the strings are same (equal) then this
function would return 0 otherwise it may return a negative or positive value based on the comparison.
If string1 < string2 -result in a negative value.
If string1 > string2 -return positive value.
If string1 == string2 -result is 0(zero)
#include <stdio.h>
#include <string.h>
void main()
{
char s1[20] = "BeginnersBook";
char s2[20] = "BeginnersBook.COM";
if (strcmp(s1, s2) ==0)
{
printf("string 1 and string 2 are equal");
}else
Output:
{ string 1 and 2 are different
printf("string 1 and 2 are different");
}getch();}
strncmp(s1,s2,n);
It compares both the string till n characters or in other words it compares first n characters
#include <stdio.h> of both the strings.
#include<conio.h>
#include <string.h>
void main()
{
char s1[20] = "BeginnersBook";
char s2[20] = "BeginnersBook.COM";
/* below it is comparing first 8 characters of s1 and s2*/
if (strncmp(s1, s2, 8) ==0)
{
printf("string 1 and string 2 are equal");
}else
{
printf("string 1 and 2 are different"); Output:
} string1 and string 2 are equal
getch();
}
strchr(s1,ch);
It searches string str for character ch.
#include <stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
char mystr[30] = "I’m an example of function strchr";
printf ("%s", strchr(mystr, 'f'));
getch(); Output:
f function strchr
}
strstr(s1,s2);
It is similar to strchr, except that it searches for string srch_term instead of
a single char.
#include <stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
char inputstr[70] = "String Function in C at BeginnersBook.COM";
printf ("Output string is: %s", strstr(inputstr, “Begi”));
getch();
}
Output:
Output string is: BeginnersBook.COM
Program to check palindrome string
#include <stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
char a[100], b[100];
printf("Enter a string to check if it is a palindrome\n");
gets(a);
strcpy(b, a); // Copying input string
strrev(b); // Reversing the string
if (strcmp(a, b) == 0) // Comparing input string with the reverse string
printf("The string is a palindrome.\n");
else
printf("The string isn't a palindrome.\n");
getch();
}
Important points for Declaration of string
• In declaration of string size must be required to mention otherwise it gives
an error.
• char str[]; // Invalid
• char str[10]; // Valid
• In declaration of the string size must be unsigned integer value (not -ve or
zero value) which is greater than zero only.
• char str[]; // Invalid
• char str[0]; // Invalid
• char str[-1]; // Invalid
• char str[10]; // Valid
Important points for Initialization of the string
• In Initialization of the string if the specific number of character are not initialized it then rest
of all character will be initialized with NULL.
char str[5]={'5','+','A'};
str[0]; ---> 5
str[1]; ---> +
str[2]; ---> A
str[3]; ---> NULL
str[4]; ---> NULL
• In initialization of the string we can not initialized more than size of string elements.
• char str[2]={'5','+','A','B'}; // Invalid
• In initialization of the string the size is optional in this case how many variable elements are
initialized it, that array element will created.
• char str[]={'5','+','A','B'}; // Valid
• sizeof(str) --> 4byte
• Thank you