Document 7830280

Download Report

Transcript Document 7830280

CGS 3460
Strings

A sequence of characters delimited by “ “ (not part of the
string)
 “hello”
 “Neko”
 “This is some random sentence that I typed!”


C does not have a string type
It uses an array of characters
 Array contains a null character (‘\0’) to denote the end of the string

Uses %s to print
CGS 3460
Example


Stores “hello” in character array of size 10
Only represent strings of length 9
 Unless we increase the size of str

%s specifes string format specifier
 causes the printf function to step through the character
array and print each character until it encounters the null
character
 No null character (‘\0’) could print undesirable results

// declare a character array
//that can hold 10 characters
char str[10];
str[0] = 'h';
str[1] = 'e';
str[2] = 'l';
str[3] = 'l';
str[4] = 'o';
str[5] = '\0'; // null character
// now print the string to display
printf ("%s \n", str);
Tedious way of initalizing strings
hello
CGS 3460
Strings

Initialization
 char word[ ]="hello";
 char word[ ]={"hello“};
word[0]
h
 char word[10] = “hello”;
word[0]  char word[10] = {“hello”};
h
e
l
l
o
\0
e
l
l
o
\0
CGS 3460
Getting String Input


Several ways – scanf is simplest but most dangerous
Example take input and print it
// demonstrates string input
#include <stdio.h>
main ()
{
// variables declaration
char name[11];
// get input from user
printf ("Your name (10 letters max):\n");
scanf ("%s", &name);
printf ("Hello %s \n", name);
}
Your name (10 letters max):
Neko
Hello Neko
CGS 3460
char str[ ] = “do it”;
int i, len;
i = 0;
len = 0;
while (str[i] != '\0')
{
i = i + 1;
}
len = i;
return len;
Find length of a string
CGS 3460
Compare Two Strings
char str1[ ] = “do its”, str2[] = “do it”;
int i = 0;
while (str1[i] == str2[i] && str1[i] != '\0' && str2[i] != '\0')
{
i = i + 1;
}
if (str1[i] == str2[i])
{
return 1;
}
return 0;
CGS 3460
String Library

C provides a set of functions to manipulate strings.
 pp 470 – 472 in book


These functions are located in the library file string.h.
If you want to make use of the string library, you first
have to include it in your programs using the #include
directive
 #include <string.h>
CGS 3460
strlen




int strlen (char str[])
This function determines the length of a string.
Takes a null-terminated character array as input
Returns a count of the number of characters till the first
null value is encountered.
CGS 3460
strlen - example
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[] = "This is a string";
printf("length(%s) = %i\n", str, strlen(str));
return 0;
}
length(This is a string) = 16
CGS 3460
strlen – example2
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[] = "";
printf("length(%s) = %i\n", str, strlen(str));
return 0;
}
length() = 0
CGS 3460
strcmp



int strcmp (char str1[], char str2[])
This function compares two strings lexicographically
(dictionary order)
Returns negative number if the first string is less than
second string
 Less than means comes before in the dictionary


Returns 0 if the two strings are equal
Returns positive number if the first string is greater than
second string
 Greater than means comes after in the dictionary

Not case sensitive
CGS 3460
strcmp – example 1
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[] = "boot";
char str2[] = "book";
printf("strcmp(%s, %s) = %i\n", str1, str2, strcmp(str1, str2));
printf("strcmp(%s, %s) = %i\n", str2, str1, strcmp(str2, str1));
return 0;
}
strcmp(boot, book) = 9
strcmp(book, boot) = -9
CGS 3460
strcmp – example 2
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[] = “boot";
char str2[] = “foot";
printf("strcmp(%s, %s) = %i\n", str1, str2, strcmp(str1, str2));
printf("strcmp(%s, %s) = %i\n", str2, str1, strcmp(str2, str1));
return 0;
}
strcmp(boot, foot) = -4
strcmp(foot, boot) = 4
CGS 3460
strcmp – example 3
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[] = “boot";
char str2[] = “boot";
printf("strcmp(%s, %s) = %i\n", str1, str2, strcmp(str1, str2));
printf("strcmp(%s, %s) = %i\n", str2, str1, strcmp(str2, str1));
return 0;
}
strcmp(boot, boot) = 0
strcmp(boot, boot) = 0
CGS 3460
strcmp – example 4
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[] = “Boot";
char str2[] = “boot";
printf("strcmp(%s, %s) = %i\n", str1, str2, strcmp(str1, str2));
printf("strcmp(%s, %s) = %i\n", str2, str1, strcmp(str2, str1));
return 0;
}
strcmp(Boot, boot) = -32
strcmp(boot, Boot) = 32
CGS 3460
strcpy

int strcpy (char dest[], char src[])

This function copies the string in the character array src[]
into the character array dest[].
It assumes that dest[] is big enough to hold the string in
src[].

 It assumes the size of dest >= strlen(src) + 1
CGS 3460
strcpy – example
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[] = "say hello to my little friend";
char str2[100];
strcpy(str2, str1);
printf("strcmp(%s, %s) = %i\n", str1, str2, strcmp(str1, str2));
printf("strcmp(%s, %s) = %i\n", str2, str1, strcmp(str2, str1));
return 0;
}
strcmp(say hello to my little friend, say hello to my little friend) = 0
strcmp(say hello to my little friend, say hello to my little friend) = 0
CGS 3460
strcat




int strcat (char dest[], char src[])
This function appends the second string to the end of the
first string.
The string in the character array src[] is appended to the
end of the string in the character array dest[].
It assumes that dest[] is big enough to accomodate the
string in src[].
CGS 3460
strcat – example
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[100] = "say hello";
char str2[] = " to my little friend";
printf("str1 before: %s\n", str1);
strcat(str1, str2);
printf("str1 after: %s\n", str1);
return 0;
}
str1 before: say hello
str1 after: say hello to my little friend
CGS 3460
Math Library



Supplies function definitions for common mathematical
operations
#include <math.h>
pp482 – 487 in book
CGS 3460
<math.h> - Functions







double ceil(x) – rounds up to nearest integer value
double floor(x) – rounds down to nearest integer value
double round(x) – rounds to nearest integer value
double fabs(x) – calculate absolute value
double cos(r) – r is in radians
double sin(r) – r is in radians
double tan(r) – r is in radians
CGS 3460
<math.h> - Functions(cont)




double exp(x) – calculate e^x
double pow(x, y) – returns x^y
double log(x) – returns ln(x)
double log10(x) – returns log10(x)
CGS 3460
Strings to Numbers





Sometimes you have a number as a character array
Then you want it as a number type
C provides functions for converting character arrays to
numbers
p 479 – 481
#include<stdlib.h>
CGS 3460
Miscellaneous Functions



Random useful functions
pp 490 – 491
#include<stdlib.h>
CGS 3460
<stdlib.h> Functions





int atoi(s) – atoi(“1234”) ;  1234
double atof(s) – atoi(“12.34”) ;  12.34
int abs(n) - also has (labs and llabs)
void exit(n)
int rand(void) – returns pseudo random number, x
 0<= x <= RAND_MAX
 How to get number between 0 and 1?


int srand(unsigned seed) – seeds the rng
int system(s)
CGS 3460
<stdio.h>



int sprintf(char* s, const char* format, …);
Prints provided string to s and ends with ‘\0’
sprintf(s, “%i”, val);
 s is char[ ] large enough to hold the printed string
 val is an integer value