built-in functions for ntcas

32
http://cs.mst.edu Built-in Functions for NTCAs

Upload: sora

Post on 07-Jan-2016

28 views

Category:

Documents


0 download

DESCRIPTION

Built-in Functions for NTCAs. strlen. char array[10 ] = “Hello”; int length = strlen (array); cout

TRANSCRIPT

Page 1: Built-in Functions for NTCAs

http://cs.mst.edu

Built-in Functions for NTCAs

Page 2: Built-in Functions for NTCAs

http://cs.mst.edu

strlen char array[10] = “Hello”; int length = strlen(array); cout << length;

Page 3: Built-in Functions for NTCAs

http://cs.mst.edu

strlen char array[10] = “Hello”; int length = strlen(array); cout << length;

h e l l o \0 ? ? ? ?

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Page 4: Built-in Functions for NTCAs

http://cs.mst.edu

strlen char array[10] = “Hello”; int length = strlen(array); cout << length;

h e l l o \0 ? ? ? ?

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

Page 5: Built-in Functions for NTCAs

http://cs.mst.edu

5

Page 6: Built-in Functions for NTCAs

http://cs.mst.edu

strcpy char source[10] = “Hey”; char target[10] = “Boy Howdy”; strcpy(target, source);

Page 7: Built-in Functions for NTCAs

http://cs.mst.edu

strcpy char source[10] = “Hey”; char target[10] = “Boy Howdy”; strcpy(target, source);

h e y \0 ? ? ? ? ? ?

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

source:

Page 8: Built-in Functions for NTCAs

http://cs.mst.edu

strcpy char source[10] = “Hey”; char target[10] = “Boy Howdy”; strcpy(target, source);

h e y \0 ? ? ? ? ? ?

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

source:

B o y H o w d y \0

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

target:

Page 9: Built-in Functions for NTCAs

http://cs.mst.edu

strcpy char source[10] = “Hey”; char target[10] = “Boy Howdy”; strcpy(target, source);

h e y \0 ? ? ? ? ? ?

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

source:

h e y \0 H o w d y \0

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

target:

Page 10: Built-in Functions for NTCAs

http://cs.mst.edu

strcpy ... strcpy(target, “Hi”); strcpy(target, “BurritoToppings”;

H i \0 \0 H o w d y \0

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

target:

Page 11: Built-in Functions for NTCAs

http://cs.mst.edu

strcpy ... strcpy(target, “Hi”); strcpy(target, “BurritoToppings”;

B u r r i t o T o p

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

target:

Page 12: Built-in Functions for NTCAs

http://cs.mst.edu

strcat char source[10] = “There”; char target[10] = “Hi”; strcat(target, source);

Page 13: Built-in Functions for NTCAs

http://cs.mst.edu

strcat char source[10] = “There”; char target[10] = “Hi”; strcat(target, source);

T h e r e \0 ? ? ? ?

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

source:

Page 14: Built-in Functions for NTCAs

http://cs.mst.edu

strcat char source[10] = “There”; char target[10] = “Hi”; strcat(target, source);

T h e r e \0 ? ? ? ?

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

source:

H i \0 ? ? ? ? ? ? ?

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

target:

Page 15: Built-in Functions for NTCAs

http://cs.mst.edu

strcat char source[10] = “There”; char target[10] = “Hi”; strcat(target, source);

T h e r e \0 ? ? ? ?

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

source:

H i T h e r e \0 ? ?

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

target:

Page 16: Built-in Functions for NTCAs

http://cs.mst.edu

strcat ... strcat(target, “!!”); strcat(target, “!”);

H i T h e r e \0 ? ?

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

target:

Page 17: Built-in Functions for NTCAs

http://cs.mst.edu

strcat ... strcat(target, “!!”); strcat(target, “!”);

H i T h e r e ! ! \0

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

target:

Page 18: Built-in Functions for NTCAs

http://cs.mst.edu

strcat ... strcat(target, “!!”); strcat(target, “!”);

H i T h e r e ! ! !

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

target:

Page 19: Built-in Functions for NTCAs

http://cs.mst.edu

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”;

cout<<strcmp(ntca1, ntca2); cout<<strcmp(ntca1, ntca3); cout<<strcmp(ntca3, ntca1); cout<<strcmp(ntca1, “bobby”);

if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

Page 20: Built-in Functions for NTCAs

http://cs.mst.edu

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”;

cout<<strcmp(ntca1, ntca2); cout<<strcmp(ntca1, ntca3); cout<<strcmp(ntca3, ntca1); cout<<strcmp(ntca1, “bobby”);

if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

Page 21: Built-in Functions for NTCAs

http://cs.mst.edu

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”;

cout<<strcmp(ntca1, ntca2); // 0 cout<<strcmp(ntca1, ntca3); cout<<strcmp(ntca3, ntca1); cout<<strcmp(ntca1, “bobby”);

if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

Page 22: Built-in Functions for NTCAs

http://cs.mst.edu

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”;

cout<<strcmp(ntca1, ntca2); // 0 cout<<strcmp(ntca1, ntca3); cout<<strcmp(ntca3, ntca1); cout<<strcmp(ntca1, “bobby”);

if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

Page 23: Built-in Functions for NTCAs

http://cs.mst.edu

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”;

cout<<strcmp(ntca1, ntca2); // 0 cout<<strcmp(ntca1, ntca3); // 1 cout<<strcmp(ntca3, ntca1); cout<<strcmp(ntca1, “bobby”);

if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

Page 24: Built-in Functions for NTCAs

http://cs.mst.edu

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”;

cout<<strcmp(ntca1, ntca2); // 0 cout<<strcmp(ntca1, ntca3); // 1 cout<<strcmp(ntca3, ntca1); // -1 cout<<strcmp(ntca1, “bobby”);

if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

Page 25: Built-in Functions for NTCAs

http://cs.mst.edu

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”;

cout<<strcmp(ntca1, ntca2); // 0 cout<<strcmp(ntca1, ntca3); // 1 cout<<strcmp(ntca3, ntca1); // -1 cout<<strcmp(ntca1, “bobby”);

if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

Page 26: Built-in Functions for NTCAs

http://cs.mst.edu

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”;

cout<<strcmp(ntca1, ntca2); // 0 cout<<strcmp(ntca1, ntca3); // 1 cout<<strcmp(ntca3, ntca1); // -1 cout<<strcmp(ntca1, “bobby”); // -1

if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

Page 27: Built-in Functions for NTCAs

http://cs.mst.edu

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”;

cout<<strcmp(ntca1, ntca2); // 0 cout<<strcmp(ntca1, ntca3); // 1 cout<<strcmp(ntca3, ntca1); // -1 cout<<strcmp(ntca1, “bobby”); // -1

if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

Page 28: Built-in Functions for NTCAs

http://cs.mst.edu

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”;

cout<<strcmp(ntca1, ntca2); // 0 cout<<strcmp(ntca1, ntca3); // 1 cout<<strcmp(ntca3, ntca1); // -1 cout<<strcmp(ntca1, “bobby”); // -1

if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

Page 29: Built-in Functions for NTCAs

http://cs.mst.edu

strcmp char ntca1[20] = “bob”; char ntca2[20] = “bob”; char ntca3[20] = “Bob”;

cout<<strcmp(ntca1, ntca2); // 0 cout<<strcmp(ntca1, ntca3); // 1 cout<<strcmp(ntca3, ntca1); // -1 cout<<strcmp(ntca1, “bobby”); // -1

if (!(strcmp(ntca1, ntca2)) cout<<”these strings are identical”<<endl; else cout<<”these strings are different”<<endl;

Take Note!

Page 30: Built-in Functions for NTCAs

http://cs.mst.edu

Dangers char source[10] = “Walk This Way”; char target[15]; strcpy(target, source); strcat(target, source);

Page 31: Built-in Functions for NTCAs

http://cs.mst.edu

Also Available strncpy strncmp strncat

Page 32: Built-in Functions for NTCAs

http://cs.mst.edu

End of Session