Practice7 - Reminder #include...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Reminder #include <stdio.h> void func1( int arr, int size) } int itr; for (itr=arr[0];itr<arr[size-1];itr++) printf("%d ",arr[itr]); } int main() } int ar1={0,1,2}; func1(ar1,2); return 0; } Strings A sequence of characters Stored, as might be expected, in an array of chars Another way to initialize: char A=blabla; Strings char arr=blabla; /*length 6 string*/ char arr=a; /* legnth 1 string */ char c=a; /* a single character */ Strings Problem how can we know where a string ends It may be much shorter than the array where its stored The Terminator Strings terminate with NULL character, signed by \0 (ascii code 0) This is a convention that is used for annotating where the string ends It means that in order to hold a string of 7 chars we need an array of length at least 8 So char arr=blabla; is equivalent to char A = {b, l, a, b, l, a, \0}; Printing strings printf allows printing whole strings at once using %s char str[200]; /* */ printf(%s\n, str); This will print the contents of str cell by cell until a \0 is encountered this may be less than 200, but also more The fool on the null #include <stdio.h> int main( void ) { char str="I'm a full string"; printf("%s\n",str); str[7]='o'; str[8]='o'; printf("%s\n",str); str[11]='\0'; printf("%s\n",str); str[11] = s; printf("%s\n", str); return 0; } Reading-in strings There are several ways of accepting strings as input from the user The obvious way to go is read character by character using getchar() Example Read_String_with_getchar.c Reading-in strings - scanf A simpler way is to use scanf To read in a string to a variable str, write scanf(%s, str); Note theres no & sign!!! Scanf reads-in letters until a space or newline is encountered The maximum length can be stated in the parentheses scanf(%10s, str); This will read in at most 10 letters, plus the \0 sign (so str should have place for 11 characters) Example Read_String_with_scanf.c Comparing strings We cannot just compare strings contents by char A[7]=Hello; char B[7]=Hello; if(A==B) { } Because A and B are addresses of A[0] and B[0] A==B only if A and B are the same string in memory In order to compare the contents we must scan char by char H e l l o \0 ....
View Full Document

Page1 / 46

Practice7 - Reminder #include...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online