l31 - char arrays 2d

Upload: svedanth8

Post on 02-Mar-2018

237 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/26/2019 l31 - Char Arrays 2d

    1/14

    S T R I N G S

    2 D Character arrays

  • 7/26/2019 l31 - Char Arrays 2d

    2/14

    Overview:Strings

    Declaration and initializationchar string_name[size];

    char myword[ ] = { 'H', 'e', 'l', 'l', 'o', '\0' };

    char result[14] =the result is

    Reading Stringscin >> 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 2

  • 7/26/2019 l31 - Char Arrays 2d

    3/14

    TwoDimensionalCharacterArrays

    7/13/2012 Department of CSE 3

    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/26/2019 l31 - Char Arrays 2d

    4/14

    ArraysofStrings

    Heres an example, that puts the names of thedays of the week in an array:

    7/13/2012 Department of CSE 4

    // 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

  • 7/26/2019 l31 - Char Arrays 2d

    5/14

    Sortingnnamesinalphabetical

    order

    7/13/2012 Department of CSE 5

    void main()

    {

    char string[30][30],temp[30];

    int no, i, j;

    coutno;

    cout

  • 7/26/2019 l31 - Char Arrays 2d

    6/14

    CheckwhetherastringisPalindrome

    or

    not

    7/13/2012 Department of CSE 6

    void main()

    {

    char str[30];

    int i,n,j,flag=1;

    cout

  • 7/26/2019 l31 - Char Arrays 2d

    7/14

    Reversingastring

    7/13/2012 Department of CSE 7

    void main()

    {

    char str[70];

    char temp;

    int i, n=0;

    clrscr();

    cout

  • 7/26/2019 l31 - Char Arrays 2d

    8/14

    Printanalphabetindecimal[ASCII]

    &

    character

    form

    7/13/2012 Department of CSE 8

    void main()

    {

    char c;

    cout

  • 7/26/2019 l31 - Char Arrays 2d

    9/14

    Change all lower case letters into

    uppercase in a sentence

    7/13/2012 Department of CSE 9

    void main()

    {

    char string[30];

    int i,n=0;

    cout

  • 7/26/2019 l31 - Char Arrays 2d

    10/14

    FindingSubstringinMainString

    7/13/2012 Department of CSE 10

    void main(){

    int i=0,j=0,k=0,count=0;

    int l=0,k1=0,cn[10],c=0;

    char a[80],b[80];

    cout

  • 7/26/2019 l31 - Char Arrays 2d

    11/14

    FindingSubstringinMainString

    7/13/2012 Department of CSE 11

    else

    if (k1==1)

    {j=0;

    k1=0;//flag reset

    }

    else

    i++;

    //end of while

    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 matchcn[c++]=i-l+1; //pos array

    //with occurrence count in c

    j=0;

    k=1;

    //presence flag (SS)

    }

  • 7/26/2019 l31 - Char Arrays 2d

    12/14

    FindingSubstringinMainString

    7/13/2012 Department of CSE 12

    if (k==1) {

    cout

  • 7/26/2019 l31 - Char Arrays 2d

    13/14

    Tobesolved

    7/13/2012 Department of CSE 13

    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/26/2019 l31 - Char Arrays 2d

    14/14

    Summary

    2DimensionalCharacterArrays

    Problems

    7/13/2012 Department of CSE 14