Professional Documents
Culture Documents
Strings
Topics:-
• Concept of Array
• One and Two Dimensional Arrays
• Declaration and Initialization of Arrays
• String
• String Storage
• Built-in-string functions
What is Array?
• An array is a fixed-size sequential collection of elements of same data types
that share a common name.
• It is simply a group of data types.
• An array is a derived data type.
• An array is used to represent a list of numbers , or a list of names.
Example of Arrays:
• A variable which represent the list of items using two index (subscript) is called
two-dimensional array.
• In Two dimensional arrays, the data is stored in rows and columns format.
• For example:
int table[2][3];
Declaration of Two-dimensional Arrays :
• The general form of two dimensional array declaration is :
type array-name[row_size][column_size];
• Here the type specifies the data type of elements contained in the array,
such as int, float, or char.
• The size should be either a numeric constant or a symbolic constant.
Initialization of Two-dimensional Arrays :
• The general form of initializing two-dimensional array is :
type array-name[row_size][column_size] = {list of values};
Example :
int table[2][3] = {0,0,0,1,1,1};
• Here the elements of first row initializes to zero and the elements of
second row initializes to one.
• This above statement can be written as :
int table[2][3] = {{0,0,0}, {1,1,1}};
• In two-dimensional array the row_size can be omitted.
Example :
int table[ ][3] = {{0,0,0}, {1,1,1}};
• If the values are missing in an initializer, they are automatically set to zero.
Example :
int table[2][3] = {1,1,2};
• Here first row initialize to 1,1 and 2, and second row initialize to 0,0 and 0
automatically.
Memory Layout of Two-dimensional array :
• In Two dimensional arrays, the data is stored in rows and columns format.
• For example:
int table[2][3] = {4,9,6,1,7,3};
Example:
“ i teach C language”
List of strings operation
• We can also declare the size larger than the string size in the intializer.
char city[20]=”NEW YORK”;
• We cannot declare the size smaller than the string size in the intializer.
char city[5]=”NEW YORK”;
this will result in a compile time error.
scanf() function
M E H \0 ? ? ? ? ?
• gets is the function to read characters into string from the terminal.
• gets is the library function and available in <stdio.h> header file.
• The gets function called as under :
gets(str);
• where str is the string variable.
• The reading is terminated when the new line character (‘\n’) is encountered
and the null character is then inserted at the end of the string.
• gets function has one parameter.
Writing strings to screen
printf function
putchar
• putchar is the function to print the values of character variables.
• It takes the following form :
char ch = ‘A’ ;
putchar (ch) ;
• The function putchar has one parameter.
• We can use this function repeatedly to print a string of characters stored in
array using a loop.
Example :
char name = “VINAYAK INFOTECH”;
for (i=0; i<=5; i++)
{
putchar(name[i]);
}
puts functions :
• puts is the function to print the values of string variables.
• puts is the library function and available in <stdio.h> header file.
• The puts function called as under :
puts(str);
• where str is the string variable.
• puts function has one parameter.
Example :
char line[80];
gets(line);
puts(line);
• This example read a line of text from the keyboard and displays it on the
screen.
String Handling Functions or Built-in String
Functions or String Manipulation Functions
Functions Action
• Example :
String2= B Y E \0
Sting1= G O O D \0
after execution of the statement strcat(string1, string2); will result in:
String 1= G O O D B Y E \0
String2= B Y E \0
• C also permits nesting of strcat function.
Example:
strcmp(“there”, “there”);
will return 0.
strcmp(“their”, “there”);
• will return the value -9 which is the numeric difference between ASCII “i”
and ASCII “r”.
Example :
• After execution this statement,
n = strlen(“VINAYAK INFOTECH”);
n = 16
• The strncpy function takes the form :
strncpy(string1, string2, n);
• Where string1, string2 are character arrays and n is integer constant.
• Here the strncpy function copies only the left most n characters of
string2 to string1.
Find length of string using strlen( ) function
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int str1;
char str;
clrscr();
printf("\nEnter the string: ");
gets(str);
str1=strlen(str);
printf("\nThe length of the string is %d.",strlength);
getch();
}
OUTPUT:
Enter the string: hello world
The length of the string is 11
Write a program to join two strings
#include <stdio.h>
#include <string.h>
void main()
{
char a[100], b[100];
clrscr();
printf("Enter the first string:\n");
scanf(“%s”,&a);
printf("Enter the second string:\n");
scanf(“%s”,&b);
strcat(a,b);
printf("String obtained on concatenation is %s\n",a);
getch();
}
OUTPUT:
Enter the first string:
Hello
Enter the second string:
World
String obtained on concatenation is World
Write a program convert character into ToggLe character
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf("Enter any character : ");
scanf("%ch", &ch);
if(ch>='A' &&ch<='Z')
{
ch=ch+32;
}
else if(ch>'a' &&ch<='z')
{
ch=ch-32;
}
printf("Convert case of character : %c",ch);
getch();
}
OUTPUT:
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[100],s2[100];
int i;
printf("\nEnter the string :");
gets(s1);
i=0;
while(s1[i]!=' ')
{
s2[i]=s1[i];
i++;
}
s2[i]=“'';
printf("\nCopied String is %s ",s2);
getch();
}
OUTPUT:
Enter the string : hello
Copied String is hello
THANK YOU