ࡱ> RTQ 8*bjbjqq .Hee8"/////CCCC$gTC!8X!Z!Z!Z!Z!Z!Z!$#%b~!/~!//!!!!B//X!!X!!!!HNC!D!!0!!&r&!&/!<!~!~!!!& : Strings in C Basically, strings are character arrays in C. However, that isn't the complete picture. It would be nice if strings didn't always have to be the same length, as character arrays are. In order to deal with this issue, strings in C, by default, are null terminated. This means that the last character storing a string is the null character, '\0'. For example, the following is a valid way to initialize a string to store "hello"; char word[20]; word[0] = 'h'; word[1] = 'e'; word[2] = 'l'; word[3] = 'l'; word[4] = 'o'; word[5] = '\0'; In this example, the character array word is storing a string of length 5. Notice that we actually store six characters here. This means that a character array of size 20 can actually store a string with a maximum length of 19, NOT 20, since the last character in the array must be reserved for the null character. Another way to read in a string is from the keyboard directly into a character array: scanf("%s", word); This reads in all the characters typed in until whitespace is read in and automatically adds the null character to the end of what is read in. One consequence of this idea is that the literals 'a' and "a" are different. The first is a single character, the second is stored in a character array of at least size two, where the last character is the null character. Contest Question Example Given a string and a character, count how many times the character appears in the string. The input file will start with a single positive integer, n, representing the number of input cases. The following n lines will contain each case, one per line. Each of these lines begins with a string of lowercase alphabetic letters of no more than 100 characters. This is followed by a space and a single character, for which to search. For each case, output a sentence of the following form: The letter L appears in the word W X times. where L is the letter for the input case, W is the word for the input case and X is the number of times L appears in W. Read the input from charcount.in. #include #include int solve(char word[], int ch); int main() { FILE* ifp = fopen("charcount.in","r"); int numcases; fscanf(ifp, "%d", &numcases); int i; for (i=0; i #include int main() { int index, freq[26]; char ch; FILE* fin; fin = fopen("input.txt", "r"); for (index = 0; index<26; index++) freq[index] = 0; fscanf(fin, "%c", &ch); while (!feof(fin)) { if (isalpha(ch)) freq[tolower(ch)-'a']++; fscanf(fin, "%c", &ch); } printf("Letter\tFrequency\n"); for (index = 0; index<26; index++) { printf("%c\t%d\n", 'a'+index, freq[index]); } fclose(fin); return 0; } If we wanted to read in string by string, we could do this in the processing part. (Well assume that no single word has more than 999 characters in it.) Here are the changes: char str[1000]; fscanf(fin, "%s", str); while (!feof(fin)) { int i; for (i=0; i, all at once. If the first is the correct relationship between the two strings, a negative integer is returned, if they are equal, 0 is returned, and otherwise, a positive integer is returned. Since we are checking to see if curname comes before firstname, the appropriate boolean condition is: strcmp(curname, firstname) < 0   1 E F _ GtLOPQx´¬}rje]eXeXPXhy"h*\h h 5>*\h5CJ \aJ hm!OJQJ\^Jhm!hm!OJQJ\^Jhm!5CJ \aJ hm!hm!\hm!hOJQJ\^Jhm!h\hm!h5>*\hm!hy"OJQJ\^Jhm!hy"\hm!5CJ$\hm!hy"5>*\ g h  0 1 l m E F _ FG$a$gdm!$a$gdm!Gst#78XYfg !Gp ^`gdm!gdm!$a$gdm!@A^q{UV#$$a$gd7gdgdm!$!$'*-.39;=?ACEGIKLFfFf$$Ifa$gdy"l $a$gd7LMPQWY[]_adgjmnsy{}Ff$$Ifa$gdy"l $a$gd7wx.;JKngd*\hy"5CJ \hy"5CJ OJQJ\^JhYhYOJQJ\^JhYOJQJ\^J h\ hY\ h7\h h*\6666666666666666666666666666666666666666666666666hH6666666666666666666666666666666666666666666666666666666666666666662 0@P`p2( 0@P`p 0@P`p 0@P`p 0@P`p 0@P`p 0@P`p8XV~_HmH nH sH tH @`@ NormalCJ_HaJmH sH tH DA D Default Paragraph FontVi@V 0 Table Normal :V 44 la (k ( 0No List j`j  Table Grid7:V0PK![Content_Types].xmlj0Eжr(΢Iw},-j4 wP-t#bΙ{UTU^hd}㨫)*1P' ^W0)T9<l#$yi};~@(Hu* Dנz/0ǰ $ X3aZ,D0j~3߶b~i>3\`?/[G\!-Rk.sԻ..a濭?PK!֧6 _rels/.relsj0 }Q%v/C/}(h"O = C?hv=Ʌ%[xp{۵_Pѣ<1H0ORBdJE4b$q_6LR7`0̞O,En7Lib/SeеPK!kytheme/theme/themeManager.xml M @}w7c(EbˮCAǠҟ7՛K Y, e.|,H,lxɴIsQ}#Ր ֵ+!,^$j=GW)E+& 8PK!Ptheme/theme/theme1.xmlYOo6w toc'vuر-MniP@I}úama[إ4:lЯGRX^6؊>$ !)O^rC$y@/yH*񄴽)޵߻UDb`}"qۋJחX^)I`nEp)liV[]1M<OP6r=zgbIguSebORD۫qu gZo~ٺlAplxpT0+[}`jzAV2Fi@qv֬5\|ʜ̭NleXdsjcs7f W+Ն7`g ȘJj|h(KD- dXiJ؇(x$( :;˹! I_TS 1?E??ZBΪmU/?~xY'y5g&΋/ɋ>GMGeD3Vq%'#q$8K)fw9:ĵ x}rxwr:\TZaG*y8IjbRc|XŻǿI u3KGnD1NIBs RuK>V.EL+M2#'fi ~V vl{u8zH *:(W☕ ~JTe\O*tHGHY}KNP*ݾ˦TѼ9/#A7qZ$*c?qUnwN%Oi4 =3ڗP 1Pm \\9Mؓ2aD];Yt\[x]}Wr|]g- eW )6-rCSj id DЇAΜIqbJ#x꺃 6k#ASh&ʌt(Q%p%m&]caSl=X\P1Mh9MVdDAaVB[݈fJíP|8 քAV^f Hn- "d>znNJ ة>b&2vKyϼD:,AGm\nziÙ.uχYC6OMf3or$5NHT[XF64T,ќM0E)`#5XY`פ;%1U٥m;R>QD DcpU'&LE/pm%]8firS4d 7y\`JnίI R3U~7+׸#m qBiDi*L69mY&iHE=(K&N!V.KeLDĕ{D vEꦚdeNƟe(MN9ߜR6&3(a/DUz<{ˊYȳV)9Z[4^n5!J?Q3eBoCM m<.vpIYfZY_p[=al-Y}Nc͙ŋ4vfavl'SA8|*u{-ߟ0%M07%<ҍPK! ѐ'theme/theme/_rels/themeManager.xml.relsM 0wooӺ&݈Э5 6?$Q ,.aic21h:qm@RN;d`o7gK(M&$R(.1r'JЊT8V"AȻHu}|$b{P8g/]QAsم(#L[PK-![Content_Types].xmlPK-!֧6 +_rels/.relsPK-!kytheme/theme/themeManager.xmlPK-!Ptheme/theme/theme1.xmlPK-! ѐ' theme/theme/_rels/themeManager.xml.relsPK] 8" Hx2")8* #G$LZ"O%8*!"8@0(  B S  ? !-58;ORSUY\qtw|~  ) / 0 3 6 9 B D O R h j x ~        $ % ) * + 1 9 : R S X Z j m } @Exz 79UZ+1>@NT]g"#9@ADEF_fgjkl   !"(%'(KQy~./BHIRT[ 39CLci   N T | !!!!"""""%"'"0":"ty8;Y\w}   ) 0 O R x    " I K j m @Exz 26ORru +2NUqt  57Z_ 7;!KRy*,BI3:TZcj"""":"3333333333333333333333333333333333333333333333333333333333333333333333333333333/2-/IKwy&*35<B^glp13RTde7"7":" y">!20'"'"3HX $P[l2!xx Strings in Cdmarino Guha ArupOh+'0  < H T `lt|Strings in CdmarinoNormal Guha Arup13Microsoft Office Word@"G,@ZB@Lۀ@JLc՜.+,0 hp   University of Central Florida>'"  Strings in C Title  !"#$&'()*+,./0123456789:;<=>?@BCDEFGHJKLMNOPSRoot Entry F0QUData %1Table-&WordDocument.HSummaryInformation(ADocumentSummaryInformation8ICompObjy  F'Microsoft Office Word 97-2003 Document MSWordDocWord.Document.89q