8 1D Arrays Strings
8 1D Arrays Strings
Array
An array in C is a variable that can store multiple
values of the same data type. It is a
fixed-size collection of similar data items stored in con
tiguous memory locations
.
The arraySize
must be an integer constant greater than zero and type ca
n be any valid C data type
05/23/2024 CSE 1001 Department of CSE 2
Arrays
int values[20];
Begin for loop
Initialize counter
Set limit for counter for ( int i=0; i<20; i++)
Increment counter
printf(“%d\n”,x[0]);
Output:
printf(“%d\n”,x[1]); 9
printf(“%d\n”,x[2]); 11
13
printf(“%d\n”,x[i]);
Program to read n elements into an array and print it
int a[10], i, n;
printf("enter no of numbers“); Output:
enter no of elements
scanf(“%d”,&n);
3
printf(“enter n numbers \n”); enter n numbers
for(i=0;i<n;i++) 9
11
scanf(“%d\n”,&x[i]);
13
Numbers entered are:
printf(“\nNumbers entered are:\n”); 9
for(i=0;i<n;i++) 11
13
printf(“%d\n”,a[i]);
Program to add two array elements and store the
corresponding elements sum in another array
void main()
if(m==n)
{ {
int a[10], b[10], c[10],n, m, i; for(i=0;i<m;i++)
printf("enter no. of numbers in { c[i]=a[i]+b[i]; }
first array\n“);
printf(“Sum of given array
scanf(“%d”,&n); elements\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]); //first array for(i=0;i<n;i++)
{ printf(“%d\n”,c[i]); }
}
printf("enter no of numbers in second else
array\n“); printf("cannot add“);
scanf(“%d”,&m); }
for(i=0;i<m; i++)
scanf(“%d”,&b[i]); //second array
Displaying elements of an array in reverse order.
printf(“%d\n”,a[i]);
WAP to delete an element from an array
Memory Requirement:
Total size =size *(sizeof(data_type));
Initialization:
type array-name [size]={list of values}
Example : a[ ]={1, 2, 3, 4, 5}
int a[20], i, j, n, temp; Enter values
n=5
printf("enter n \n“); 12345
Reversed array
scanf(“%d”, &n);
5 4 3 2 1
printf("\n Enter values for an array“); Array Reversed
array
for(i=0;i<n;i++) a[0]=1 a[0]=5
a[1]=2 a[1]=4
scanf(“%d”,&a[i]); a[2]=3 a[2]=3
a[3]=4 a[3]=2
Contd… a[4]=5 a[4]=1
Reversing an array
for(i=0, j=n-1; i<n/2; i++, j--) Example :
a[ ]={1, 2, 3, 4, 5}
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
Output:
printf("\n Reversed array: \n“);
Enter values for an array
for(i=0;i<n;i++) n=5
12345
printf(“%d\t”,a[i]); Reversed array
5 4 3 2 1
}
Linear Search
/*search procedure*/
int found=0; //setting flag
for(i=0; i<n; i++) {
Print "enter no of elements"; if(a[ i ]==key) // comparison
Input n; {
found=1;
for(i=0;i<n;i++){
pos=i+1;
Print “enter number\n"; break;
searched"; otherwise
Print “data is not found“;
Input key; // data to be searched
05/23/2024 CSE 1001 Department of CSE 23
Sorting
Arrangement of data elements in a particular order
Bubble sorting
For example
The 5 characters that compose the word "Hello" plus a final null
character ('\0') which specifies the end of the
In the second case, when using double quotes (") null character ('\0') is
appended automatically.
int main() {
printf(“%s”, question);
scanf(“%s”, &yourname);
#include <stdio.h>
int main()
To read everything that you enter from the keyboard until the
ENTER key is pressed (including space).
Syntax:
gets(string) ;
#include<string.h>
int main()
{ {
variable str
printf("\nEnter a string:\n“);
The function will continue to accept characters until enter key is pressed.
strlen ()
gives the length of the string. E.g. strlen(string)
strcpy ()
copies one string to other. E.g. strcpy(Dstr1, Sstr2)
strcmp ()
compares the two strings. E.g. strcmp(str1, str2)
strcat ()
Concatinate the two strings. E.g. strcat(str1, str2)
#include<string.h>
int main() sent[0] sent[1] sent[2] sent[3] sent[4]
{ Enter sentence
Delhi
char sent[100]; 5 (length of string sent)
int len; I
printf("enter string \n“); D
gets(sent);
len=strlen(sent);
printf(“%d\n”,len);
printf(“%c\n”,sent[len-1]);
printf(“%c\n”,sent[0]); }
#include<string.h> sent[i]=sent[i]+1;
int main() printf(“ the encrypted string is \n”);
{ puts(sent);
const int MAX = 100;
for(i=0;sent[i]!=‘\0’;i++)
char sent[MAX];
sent[i]=sent[i]-1;
int len,I;
printf(“ the decrypted string is \n”);
printf("enter sentence \n“);
puts(sent);
gets(sent);
}
will assign the contents of the string variable city2 to the string variable
city1.
The size of the array city1 should be large enough to receive the
contents of city2.
05/23/2024 CSE 1001 Department of CSE 47
strcpy(): Example
#include <stdio.h>
int main()
printf(“%s”,str2);//display str2
}
05/23/2024 CSE 1001 Department of CSE 48
Library function: strcmp()
for(i=0;i<no-1;i++)
for(j=i+1;j<no;j++)
int main()
{
{ if(strcmp(string[i],string[j])>0)
{
char string[30][30],temp[30];
strcpy(temp,string[i]);
int no, i, j;
cout<<"\nEnter the no of strings:"; strcpy(string[i],string[j]);
strcpy(string[j],temp);
cin>>no; }
cout<<"\nEnter the strings:"; }
Main string: cc aa bb
Sub-String : aa
56
Finding Substring in Main String
int main()
{
char str1[80],str2[80];
int i,j,len=0;
57
Finding Substring in Main String
for(i=0,j=0; str1[i]!='\0‘ && str2[j]!='\0'; i++)
if(str1[i]==str2[j])
j++;
else
j=0;
if(j==len)
cout<<"Substring\t"<<str2<< "\tfound;
else
cout<<"Substring not found";
58