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

l31 - Char Arrays 2d

This document discusses strings and two-dimensional character arrays. It provides examples of declaring and initializing strings, reading strings, and built-in string functions. It also demonstrates declaring and initializing 2D character arrays, arrays of strings, sorting strings alphabetically, checking for palindromes, reversing strings, and finding substrings within a main string.

Uploaded by

svedanth8
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)
44 views14 pages

l31 - Char Arrays 2d

This document discusses strings and two-dimensional character arrays. It provides examples of declaring and initializing strings, reading strings, and built-in string functions. It also demonstrates declaring and initializing 2D character arrays, arrays of strings, sorting strings alphabetically, checking for palindromes, reversing strings, and finding substrings within a main string.

Uploaded by

svedanth8
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

2 D Character arrays

Overview:Strings
Declaration and initialization
char string_name[size];
char myword[ ] = { 'H', 'e', 'l', 'l', 'o', '\0' };
char result[14] =the result is
Reading Strings
cin >> myword;
gets(string)
cin.get(array_name, size, stop_char)
String Functions (in-built)
n=strlen(string)
strcpy(destination, source)
strcmp(string1,string2);
strcat(string1,string2);
7/13/2012

Department of CSE

TwoDimensionalCharacterArrays
These are array of arrays. 2-D character array
consists of strings as its individual elements.
Declaration :char a[10][10];
Initialization:char a[10][20]={
aaa,
bbb,
ccc
};
7/13/2012

Department of CSE

ArraysofStrings
Heres an example, that puts the names of the
days of the week in an array:
// array of strings

void main()
{
const int DAYS = 7; //number

of strings in array

const int MAX = 10;

//maximum size of each string

//array of strings

char star[DAYS][MAX] = {
Sunday, Monday, Tuesday,
Wednesday, Thursday, Friday,
Saturday };
for(int j=0; j<DAYS; j++) //display

every string

cout << star[j] << endl;


}

7/13/2012

Department of CSE

Sortingn namesinalphabetical
order
for(i=0;i<no-1;i++){
void main()
{
char string[30][30],temp[30];
int no, i, j;
cout<<"\nEnter the no of strings:";
cin>>no;
cout<<"\nEnter the strings:";
for(i=0;i<no; i++)
gets(string[i]);

7/13/2012

for(j=i+1;j<no;j++) {
if(strcmp(string[i],string[j])>0)

strcpy(temp,string[i]);
strcpy(string[i],string[j]);
strcpy(string[j],temp);
}

}
cout<<"\nThe sorted array is:";
for(i=0;i<no;i++)
puts(string[i]);
}

Department of CSE

CheckwhetherastringisPalindrome
ornot
void main()
{
char str[30];
int i,n,j,flag=1;
cout<<"\nEnter the string:";
gets(str);
for(i=0;str[i]!='\0';i++);
n=i; //n=strlen(str)

7/13/2012

for(i=0;i<n/2;i++){
if(str[i]!=str[n-i-1])
{
flag=0;
break;
}
}
if(flag==1)
cout<<"\nString is a Palindrome";
else
cout<<"\nString is not a Palindrome";
}
Department of CSE

Reversingastring
void main()
{
char str[70];
char temp;
int i, n=0;
clrscr();
cout<<"\nEnter the string:";
gets(str);
for(i=0;str[i]!='\0';i++)
n++;

7/13/2012

for(i=0;i<n/2;i++)
{
temp=str[i];
str[i]=str[n-i-1];
str[n-i-1]=temp;
}
cout<<"\nThe reversed string is:";
puts(str);
getch();
}

Department of CSE

Printanalphabetindecimal[ASCII]
&characterform
void main()
{
char c;
cout<<"\n";
for(c=65;c<=122;c++)
{
if(c>90 && c<97)
continue;
cout<<c<<"-"<<(int)c<<" ";
}
cout<<"\n";
}
7/13/2012

Department of CSE

Change all lower case letters into


uppercase in a sentence
void main()
{
char string[30];
int i,n=0;
cout<<"\nEnter the string";
gets(string);

for(i=0;i<n;i++)
{
if(string[i]>=97 && string[i]<=122)
string[i]=string[i]-32;
}

for(i=0;string[i]!='\0';i++)
n++;

puts(string);
getch();
}

7/13/2012

Department of CSE

FindingSubstringinMainString
Main string: cc aa bb aa aa
Sub-String : aa
void main(){

Enter main string:- cc aa bb aa aa


Enter sub-string : aa
Substring is present 3 times at positions 4 10 13

int i=0,j=0,k=0,count=0;
int l=0,k1=0,cn[10],c=0;
char a[80],b[80];
cout<<"\nEnter main string:-\n";
gets(a);
cout<<"\nEnter sub-string:-\n";
gets(b);
l=strlen(b); //length of substring
7/13/2012

Department of CSE

10

FindingSubstringinMainString
while (a[i]!='\0) {//outer loop for MS
if (a[i]==b[j]) {
i++;
j++;
k1=1; //character match flag
if (j==l) { //check for all chars match
cn[c++]=i-l+1; //pos array
//with occurrence count in c

j=0;
k=1; //presence flag (SS)

else {
if (k1==1){
j=0;
k1=0; //flag reset
}
else
i++;
}
} //end of while

}
7/13/2012

Department of CSE

11

FindingSubstringinMainString
if (k==1) {
cout<<"\nSubstring is present "<< c <<" time(s) at position(s)\t";
for(i=0;i<c;i++)
cout<<cn[i]<<"\t";
}
else {
if (k==0)
cout<<"\nGiven sub-string is not present in the main string.";
}
}//end of program

7/13/2012

Department of CSE

12

Tobesolved
Write programs to

Read a string (sentence) and remove the blank spaces and


display the resultant string without any blank spaces.

Read a string (sentence) and replace the blank space with B


and display the resultant string.

7/13/2012

Department of CSE

13

Summary
2DimensionalCharacterArrays
Problems

7/13/2012

Department of CSE

14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy