You are on page 1of 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 <stdio.h> /* 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 the variable s2 when this program was written in 2003. However, on my current desktop PC in 2011 it overwrote 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 example the result between the square brackets might be [123 ] or it might be [ 123] which means it is now right justified. In some cases it will overwrite s2 when you enter too many values and other times it won’t

2 In this case we changed output to %5s, but input remains %s and we enter 7 characters. #include <stdio.h> /* 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");
Document1--- by rt on 3 November 2012 1 of 7

h> /* 09s4. printf ("\n\n"). a). printf (" ====> Using printf with %%5s"). s3. char s3[6]="ccccc". Is there any effect if we enter 6 characters the exact size of the array? #include <stdio. printf (" ====> Using printf with %%5s").c */ main() { char s1[6]= "aaaaa". s1). printf ("\n\n"). char s3[6]="ccccc".h> /* 09s3. #include <stdio. s2. but this time it is on the scanf. s1.by rt on 3 November 2012 2 of 7 . } Did anything get overwritten? Is there a way to control not getting overwritten? Document1--. s1). s3. scanf ("%s". printf ("Enter exactly 7 characters into s1 using %%s ==> "). a). printf ("\n\n"). a). 3. printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n". printf ("\n\n").\ printf ("\n\n").c */ main() { char s1[6]= "aaaaa". printf ("\n\n"). s1. s2. } 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 control output. printf ("Enter exactly 6 characters into s1 using %%s ==> "). s3. s2. s2[6]="bbbbb". printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n". scanf ("%5s". int a=9.printf (" ====> Using %%5s"). int a=9. s2[6]="bbbbb". printf ("s1 is [%5s] s2 is [%s] } Was there any effect? s3 is [%s] a is [%d]\n".\ printf ("\n\n"). s1.

printf (" ====> Using printf with %%s").c */ main() { char s1[6]= "aaaaa". s1. s2. s1).c */ main() { char s1[6]= "aaaaa". s1. Note whether the scanf waited for you to enter any data. int a=9.\ printf ("\n\n"). } 6 What happens if the input has a space but total characters are less than defined array size? #include <stdio. printf ("\n\n"). s3. a). s1). int a=9. s3. scanf ("%s". Did anything happen to the 2? What can you conclude? Document1--. printf ("\n\n"). s1. a). Then another printf was added to see if there was any effect. s2). printf (" ====> Using printf with %%5s"). printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n". s2. printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n". printf (" ====> Using printf with %%5s").\ printf ("\n\n"). s2. a). s3. scanf ("%5s". char s3[6]="ccccc". s1. printf ("\n\n"). s2[6]="bbbbb". #include <stdio.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 into s2. a). printf ("Notice that the value in s1 is right justified\n\n").h> /* 09s5. printf ("\n\n"). printf ("Enter exactly the 2 characters 1 2 with a space between into s1 ==> ").by rt on 3 November 2012 3 of 7 . s3. printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n". char s3[6]="ccccc". printf ("Enter exactly 7 characters into s1 using %%s ==> "). } What is now in s1? Notice it is right justified. s2. scanf ("%5s".h> /* 9s6. s2[6]="bbbbb". printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n". printf ("\n\n").

s2[6]="bbbbb".  scanf ("%5[]s". s1).c */ main() { char s1[6]= "aaaaa". a). Try the program again. And so does the 5 as in %5s. s2. s3. This time change the scanf to control the input to the following  scanf ("%5[A-Z a-z]s". (Use a space and not the word space. ? #include <stdio.c */ main() { char s1[6]= "aaaaa". s1).h> /* 9s9. except remove the space in the scanf between the brackets. printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n". int a=9. printf ("Notice that the value in s1\n\n"). char s3[6]="ccccc". } What does this part mean [ ] in the scanf? It means inside the square brackets are the acceptable values for the scanf. printf ("Enter exactly 123 123 note the space between them into s1 ==> "). int a=9. the characters in the square brackets limit the acceptable values to store. char s3[6]="ccccc".) Notice that between the square brackets on output for variable s1 that it has changed. printf (" ====> Using printf with %%5s"). What happened this time. printf ("\n\n").\ printf ("\n\n"). s1. s2[6]="bbbbb". .7 What happens if the input has a space but total characters are greater than THE defined array size? Run program above but try it with these 2 inputs that have a space. and do you understand it? 9 From the previous examples we can conclude that what is inside the square brackets controls the input. s1).by rt on 3 November 2012 4 of 7 . 1234 12345 1234567890 1234567890 8 The same as 6 except this time the scanf has changed to  %5[ ]s ? #include <stdio. scanf ("%5[ ]s".h> /* 9s8. printf ("\n\n"). Just like the %5s limited the input to the first 5 characters entered.\ Document1--. Try it again with  spaceABCDEF.

char s3[6]="ccccc". What needs to change to be able to accept the dash (-). s3. } Enter 123 456 Enter ab cd Enter AB CD Enter AB CDEFGH What happened that was different? Try again with AB-D AB2D It stopped on the first character that did not fit the acceptable criteria. s1. s1). int a=9. a). printf ("Notice that the value in s1\n\n"). s3. printf (" ====> Using printf with %%5s"). printf ("\n\n"). The colon is hard to see but it is after the small z. printf ("Enter what was asked in question 9 into s1 ==> "). As soon as the user enters the unacceptable character it stops. s2. scanf ("%5[A-Z a-z]s". printf ("Enter what was asked in question 10 into s1 ==> "). printf ("\n\n"). #include <stdio. s1).by rt on 3 November 2012 5 of 7 . printf (" ====> Using printf with %%5s").\ printf ("\n\n").printf ("\n\n"). 10 Suppose you want to accept a colon. s2[6]="bbbbb". scanf ("%5[A-Z a-z:]s". a). s1). printf ("\n\n"). printf ("Notice that the value in s1\n\n"). printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n".c */ main() { char s1[6]= "aaaaa". s1. printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n". Document1--. } Test it with AB:C Now try and enter AB/C and see what happens. s2. printf ("\n\n").h> /* 9s10. 11 Change the scanf by adding ^: as in the example  scanf ("%5[^:A-Z a-z^:]s".

int a=9. The symbol ^ at the beginning means not any of the characters after it.by rt on 3 November 2012 6 of 7 . } Now enter A^:B It 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". printf ("Notice that the value in s1. s1). s1).c */ main() { char s1[6]= "aaaaa". int a=9. s2[6]="bbbbb". printf ("Enter what was asked in question 11 into s1 ==> "). s1. s1). printf (" ====> Using printf with %%5[^:A-Z a-z^:]s"). s2[6]="bbbbb". s2. printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n". printf ("Notice that the value in s1.c */ main() { char s1[6]= "aaaaa".h> /* 9s12.#include <stdio. a). #include <stdio. printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n". s3. s2. printf ("\n\n"). s3. printf ("Enter what was asked in question 12 into s1 ==> "). s1. printf (" ====> Using printf with %%5[^:A-Z a-z]s").\ printf ("\n\n").h> /* 9s11. } Now enter A^:B It accepted nothing. printf ("\n\n"). Document1--. char s3[6]="ccccc". a). char s3[6]="ccccc". scanf ("%5[^:A-Z a-z]s". printf ("\n\n"). "). printf ("\n\n"). scanf ("%5[A-Z a-z^:]s". printf ("\n\n"). You might have expected the A\n" " but it didn’t accept anything\n").

printf ("Enter what was asked in question 13 into s1 ==> ").. Document1--. a). } Enter A:B It stopped on the colon and took only the A Challenge First increase s1 and s2 to 11 Bonus 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 JOE BOB into s2 Why? A common way of sending data is to delimit it by a colon. then putting to commas ( . #include <stdio. printf ("Notice the value in s1.by rt on 3 November 2012 7 of 7 .c */ main() { char s1[6]= "aaaaa". ) will save sending 10 space characters. s2. s2[6]="bbbbb". char s3[6]="ccccc". printf ("\n\n"). s1). If you are downloading a spreadsheet and many of the cells are empty.The ^ at the beginning changes the effect to NOT these characters 13 Change scanf to do this  scanf ("%5[A-Z a-z][^:]". The files are called . scanf ("%5[A-Z a-z][^:]". One of the purposes is to save space with data that is in varying sizes such as the example that follows except in this case we are using a colon as a separator. Josephine Highliner 123 Adamson St Ron Tarr 123 Bestway Ave Josephine: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 is used. printf ("\n\n").h> /* 9s13. s3. "). printf ("s1 is [%5s] s2 is [%s] s3 is [%s] a is [%d]\n\n".CSV files meaning comma separated values. printf ("\n\n"). printf (" ====> Using printf with %%5[A-Z a-z][^:]s"). s1. It could be TAB or colon or slash (/). s1). int a=9.