great string examples to try

Upload: rnjai-lamba

Post on 04-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Great String Examples to Try

    1/7

    Document1--- by rt on 3 November 20121 of 7

    09-03a-Great string examples to try

    These exercises are to show you the way strings operate in C.

    1 What happens if we enter more than the size defined for the character array?

    #include

    /* 09s1.c */main(){

    char s1[6]= "aaaaa", s2[6]="bbbbb";int a=9;char s3[6]="ccccc";\

    printf ("\n\n09s1.c .....\n\n");printf ("s1 is [%s] s2 is [%s] s3 is [%s] a is [%d]\n\n",s1,s2,s3,a);

    printf ("Enter 7 characters into s1 which is defined as 6 using \%s ==> ");

    scanf ("%s", s1);printf ("\n\n");printf ("s1 is [%s] s2 is [%s] s3 is [%s] a is [%d]\n\n",s1,s2,s3,a);

    printf ("Did any variable get overwritten?\n""On mine it was a but I was expecting s2 \n\n");

    NOTE: the values overwrote usually overwrite the next memory space which happened to be for thevariable s2 when this program was written in 2003. However, on my current desktop PC in 2011 itoverwrote the variable a.

    Now try the same program but with 6 characters and then 5 characters.

    After running each of these next small programs determine what happened each time. For examplethe result between the square brackets might be [123 ] or it might be [ 123] which means it is nowright justified. In some cases it will overwrite s2 when you enter too many values and other times itwont

    2 In this case we changed output to %5s, but input remains %s and we enter 7 characters.#include

    /* 09s2.c */main(){ char s1[6]= "aaaaa", s2[6]="bbbbb";

    int a=9;char s3[6]="ccccc";\printf ("\n\n");printf ("Enter 7 characters into s1 using %%s ==> ");scanf ("%s", s1);printf ("\n\n");

  • 7/31/2019 Great String Examples to Try

    2/7

    Document1--- by rt on 3 November 20122 of 7

    printf (" ====> Using %%5s");printf ("\n\n");printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n", s1, s2, s3, a);

    }Was there any effect?

    3. Is there any effect if we enter 6 characters the exact size of the array?

    #include /* 09s3.c */main(){

    char s1[6]= "aaaaa", s2[6]="bbbbb";int a=9;char s3[6]="ccccc";\

    printf ("\n\n");printf ("Enter exactly 6 characters into s1 using %%s ==> ");scanf ("%s", s1);

    printf ("\n\n");printf (" ====> Using printf with %%5s");printf ("\n\n");printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n", s1, s2, s3, a);

    }

    What happened? Did anything still get overwritten on output?

    4 This time change the scanf to show %5s to attempt to control input? Before we tried to controloutput, but this time it is on the scanf.

    #include /* 09s4.c */main(){

    char s1[6]= "aaaaa", s2[6]="bbbbb";int a=9;char s3[6]="ccccc";\

    printf ("\n\n");printf ("Enter exactly 7 characters into s1 using %%s ==> ");scanf ("%5s", s1);

    printf ("\n\n");printf (" ====> Using printf with %%5s");printf ("\n\n");printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n", s1, s2, s3, a);

    }

    Did anything get overwritten?Is there a way to control not getting overwritten?

  • 7/31/2019 Great String Examples to Try

    3/7

    Document1--- by rt on 3 November 20123 of 7

    5 Did the extra characters get left in the buffer?To test this out another scanf is added to the end taking anything in the input buffer and putting it intos2. Then another printf was added to see if there was any effect. Note whether the scanf waited foryou to enter any data.

    #include /* 09s5.c */main()

    {char s1[6]= "aaaaa", s2[6]="bbbbb";int a=9;char s3[6]="ccccc";\

    printf ("\n\n");printf ("Enter exactly 7 characters into s1 using %%s ==> ");scanf ("%5s", s1);printf ("\n\n");printf (" ====> Using printf with %%s");printf ("\n\n");

    printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n", s1, s2, s3, a);printf (" ====> Using printf with %%5s");printf ("\n\n");printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n", s1, s2, s3, a);scanf ("%s", s2);printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n", s1, s2, s3, a);

    }

    6 What happens if the input has a space but total characters are less than defined array size?#include /* 9s6.c */

    main(){

    char s1[6]= "aaaaa", s2[6]="bbbbb";int a=9;char s3[6]="ccccc";\

    printf ("\n\n");printf ("Enter exactly the 2 characters 1 2 with a space between into s1 ==> ");scanf ("%5s", s1);printf ("\n\n");printf (" ====> Using printf with %%5s");

    printf ("\n\n");printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n", s1, s2, s3, a);printf ("Notice that the value in s1 is right justified\n\n");

    }What is now in s1?Notice it is right justified.Did anything happen to the 2?What can you conclude?

  • 7/31/2019 Great String Examples to Try

    4/7

    Document1--- by rt on 3 November 20124 of 7

    7 What happens if the input has a space but total characters are greater than THE defined arraysize?

    Run program above but try it with these 2 inputs that have a space.1234 123451234567890 1234567890

    8 The same as 6 except this time the scanf has changed to %5[ ]s ?

    #include /* 9s8.c */main(){

    char s1[6]= "aaaaa", s2[6]="bbbbb";int a=9;char s3[6]="ccccc";\

    printf ("\n\n");printf ("Enter exactly 123 123 note the space between them into s1 ==> ");scanf ("%5[ ]s", s1);printf ("\n\n");printf (" ====> Using printf with %%5s");printf ("\n\n");printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n", s1, s2, s3, a);printf ("Notice that the value in s1\n\n");

    }

    What does this part mean [ ] in the scanf? It means inside the square brackets are the acceptable

    values for the scanf. Just like the %5s limited the input to the first 5 characters entered, thecharacters in the square brackets limit the acceptable values to store..Try it again with spaceABCDEF. (Use a space and not the word space.) Notice that between thesquare brackets on output for variable s1 that it has changed.

    Try the program again, except remove the space in the scanf between the brackets. scanf ("%5[]s", s1);

    What happened this time, and do you understand it?

    9 From the previous examples we can conclude that what is inside the square brackets controls theinput. And so does the 5 as in %5s.This time change the scanf to control the input to the following scanf ("%5[A-Z a-z]s", s1); ?#include /* 9s9.c */main(){ char s1[6]= "aaaaa", s2[6]="bbbbb";

    int a=9;char s3[6]="ccccc";\

  • 7/31/2019 Great String Examples to Try

    5/7

    Document1--- by rt on 3 November 20125 of 7

    printf ("\n\n");printf ("Enter what was asked in question 9 into s1 ==> ");scanf ("%5[A-Z a-z]s", s1);printf ("\n\n");printf (" ====> Using printf with %%5s");printf ("\n\n");printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n", s1, s2, s3, a);

    printf ("Notice that the value in s1\n\n");}

    Enter 123 456Enter ab cdEnter AB CDEnter AB CDEFGHWhat happened that was different?

    Try again withAB-D

    AB2DIt stopped on the first character that did not fit the acceptable criteria.What needs to change to be able to accept the dash (-).

    10 Suppose you want to accept a colon. The colon is hard to see but it is after the small z.

    #include /* 9s10.c */main(){

    char s1[6]= "aaaaa", s2[6]="bbbbb";int a=9;char s3[6]="ccccc";\

    printf ("\n\n");printf ("Enter what was asked in question 10 into s1 ==> ");scanf ("%5[A-Z a-z:]s", s1);printf ("\n\n");printf (" ====> Using printf with %%5s");printf ("\n\n");printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n", s1, s2, s3, a);

    printf ("Notice that the value in s1\n\n");}Test it with AB:CNow try and enter AB/C and see what happens.

    As soon as the user enters the unacceptable character it stops.

    11 Change the scanf by adding ^: as in the example scanf ("%5[^:A-Z a-z^:]s", s1);

  • 7/31/2019 Great String Examples to Try

    6/7

    Document1--- by rt on 3 November 20126 of 7

    #include /* 9s11.c */main(){

    char s1[6]= "aaaaa", s2[6]="bbbbb";int a=9;char s3[6]="ccccc";\

    printf ("\n\n");printf ("Enter what was asked in question 11 into s1 ==> ");scanf ("%5[A-Z a-z^:]s", s1);printf ("\n\n");printf (" ====> Using printf with %%5[^:A-Z a-z :]s");printf ("\n\n");printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n",

    s1, s2, s3, a);printf ("Notice that the value in s1. ");

    }

    Now enter A^:BIt accepted a few extra symbols

    12 Change the scanf and put the ^: on the front and try it again

    scanf ("%5[^:A-Z a-z]s", s1);

    #include /* 9s12.c */

    main(){

    char s1[6]= "aaaaa", s2[6]="bbbbb";int a=9;char s3[6]="ccccc";

    printf ("\n\n");printf ("Enter what was asked in question 12 into s1 ==> ");scanf ("%5[^:A-Z a-z]s", s1);printf ("\n\n");printf (" ====> Using printf with %%5[^:A-Z a-z]s");

    printf ("\n\n");printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n",

    s1, s2, s3, a);printf ("Notice that the value in s1. You might have expected the A\n"

    " but it didnt accept anything\n");}Now enter A^:BIt accepted nothing.The symbol ^ at the beginning means not any of the characters after it.

  • 7/31/2019 Great String Examples to Try

    7/7

    Document1--- by rt on 3 November 20127 of 7

    The ^ at the beginning changes the effect to NOT these characters

    13 Change scanf to do this scanf ("%5[A-Z a-z][^:]", s1);

    #include /* 9s13.c */

    main(){

    char s1[6]= "aaaaa", s2[6]="bbbbb";int a=9;char s3[6]="ccccc";

    printf ("\n\n");printf ("Enter what was asked in question 13 into s1 ==> ");scanf ("%5[A-Z a-z][^:]", s1);printf ("\n\n");printf (" ====> Using printf with %%5[A-Z a-z][^:]s");

    printf ("\n\n");printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n",

    s1, s2, s3, a);printf ("Notice the value in s1. ");

    }

    Enter A:BIt stopped on the colon and took only the A

    ChallengeFirst increase s1 and s2 to 11Bonus for figuring it out if enter R TARR:JOE BOB

    14 How do we read in colon separated data just like the input above with R TARR into s1 and JOEBOB into s2Why?

    A common way of sending data is to delimit it by a colon. The files are called .CSV files meaningcomma separated values. One of the purposes is to save space with data that is in varying sizes suchas the example that follows except in this case we are using a colon as a separator. If you aredownloading a spreadsheet and many of the cells are empty, then putting to commas ( ,, ) will save

    sending 10 space characters.Josephine Highliner 123 Adamson StRon Tarr 123 Bestway AveJosephine:Highliner:123 Adamson St:Ron:Tarr:123 Bestway Ave:

    When data is moved from Excel to other software in order to preserve the cell format a separator isused. It could be TAB or colon or slash (/).