0% found this document useful (0 votes)
27 views27 pages

CS 116 Lecture 14 - Short

Uploaded by

Rama Jarrar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views27 pages

CS 116 Lecture 14 - Short

Uploaded by

Rama Jarrar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

CS 116 Computing Fundamentals

Strings (1)
Introduction

Most of the slides in this lecture are either from or adapted from the slides provided by Dr. Christina Class
Character Variable

 A character char is a single ‘character’ or


symbol.
 As literal constant it is included in single
quotation marks ‘ ’
 char x = ‘a’;

A variable of a
A literal constant
character type

4
getchar() and putchar()

 getchar() and putchar() are functions to


read / print one single character.
 they are also declared in stdio.h
 examples:
 char c = ‘4’;
 putchar(c); Print the value of c into the screen

 c = getchar(); Request the user to input a literal value to be stored into c

 putchar(c); Print the value of c into the screen

5
Reading and Printing a
character

using scanf() & printf() using getchar() & putchar()


#include <stdio.h> #include <stdio.h>

int main(void) { int main(void) {


char x; char x;

printf("Please enter a character: "); printf("Please enter a character: ");


scanf("%c", &x); x = getchar();
Equivalent Operations

printf("You entered "); printf("You entered ");


printf("%c", x); Equivalent Operations
putchar(x);

return 0; return 0;
} }

6
 Each character is stored as an int (ASCII code).
#include <stdio.h>

int main(void) {
char x;

printf("Please enter a character: ");


x = getchar();//equivalent to scanf

printf("You entered ");


putchar(x);//equivalent to printf
printf("\n");
printf("%c %d", x, x);

return 0;
}

7
String

8
 A string is a character sequence and as
literal constant included in double
quotation marks “ ”.
 In some programming languages, there is an
extra data type for strings  not in C!
 In C, a string is implemented using an array
of characters.

9
 In C programming, a string is a sequence of characters
terminated with a null character \0. For example:

 char c[] = "c string";

 When the compiler encounters a sequence of characters


enclosed in the double quotation marks, it appends a null
character \0 at the end by default.

 The previous string is similar to


 char c[] = {‘c’, ‘ ’, ‘s’, ‘t’, ‘r’, ‘n’, ‘g’, ‘\0’};

10
 If we define a character array of length 80,
we can store different strings in this array,
e.g., “hello”, “Computing Fundamentals”,
“Today is Monday”, etc.
 In order to denote the end of a string, C
stores the string termination character ‘\0’
as last character in the array.
 If we initialize a character array with a string
literal, ‘\0’ is automatically appended at the
end of the string.
11
How to declare a string

 char s[5];

13
How to initialize strings?

 In the following example, we allocate an array


of 5 characters.
 char c[] = "abcd";
 char c[] = {'a', 'b', 'c', 'd', '\0'};
 char c[5] = {'a', 'b', 'c', 'd', '\0’};

 In the following example, we allocate an array


of 50 characters with only 5 filled elements.
 char c[50] = "abcd";
14
Assigning Values to Strings

 Arrays and strings are second-class citizens


in C; they do not support the assignment
operator once it is declared. For example,
char c[100];
c = “C Programming”; //Error! Array type is not assignable

char c[100] = “C Programming”;

15
Conversion specifier for strings

 The conversion specifier for strings is %s


 Important note about using scanf() with a string:
 You can use the scanf() function to read a string.
 The scanf() function reads the sequence of characters until it
encounters whitespace (e.g., space, newline, tab, etc.).
 The string does not require an address operator when using
it as an argument for the scanf() function.

char string[20] = “Hello!”;


printf(“%s\n”, string);
scanf(“%s”, string);
printf(“%s”, string);

16
scanf() to read a string

#include <stdio.h> Run #1:

int main()
Run #2:
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
17
important!

 used in printf:
 %s displays the whole string until the string
termination character.
 used in scanf:
 %s reads all characters (and stores them in the
array) until the next whitespace.
 “Hello World” cannot be input in one string using
%s
 if the input string is longer than the array it is to
be stored in, the program can crash
18
 we can specify the max. length of characters to
be read between % and s
 char string[21];
 scanf(“%20s”, string);
 we use 20 instead of 21 in order to allow for the
string termination symbol to be stored
 all characters that are not read by scanf if used in
this way are buffered and will be used for the next
input
 if we want to avoid this:
 fflush(stdin);
after scanf empties the input buffer.

19
#include <stdio.h>

int main(void) {
char s1[20] = "Hello";
char s2[] = "CS Students!";
printf("%s %s\n", s1, s2);

printf("Please enter a string (up to 19 characters):\n");


scanf("%s", s1);

printf("%s\n%s\n", s1, s2);

return 0;
}
20
#include <stdio.h> What happen if the user
input more that 19
characters?
int main(void) {
char s1[20] = "Hello";
char s2[] = "CS Students!";
printf("%s %s\n", s1, s2);

printf("Please enter a string (up to 19 characters):\n");


scanf("%s", s1);

printf("%s\n%s\n", s1, s2);

return 0;
}
A run-time error!
21
the maximum allowed
characters?
#include <stdio.h> What happen if the user
input more that 19
characters?
int main(void) {
char s1[20] = "Hello";
char s2[] = "CS Students!";
printf("%s %s\n", s1, s2);

printf("Please enter a string (up to 19 characters):\n");


scanf("%19s", s1);

printf("%s\n%s\n", s1, s2);

return 0;
}

22
String Manipulation Functions

Using user-defined function


 On the following slides we will discuss the
implementation of some string
manipulation functions.
 Most of them are also available in the
standard library and need not be
implemented directly.
 But the discussion of these functions is a
helpful revision of many C language
elements.
24
 On all of the following slides, we assume
that the strings are correctly ended by a ‘\0’
 We equally assume that arrays are long
enough to store strings (e.g., the result
array in the stringConcat() function).

25
string length
int stringLength(char string[])
{
int i = 0;
while (string[i] != '\0') {
++i;
}
return i;
}

26
string concatenation
void stringConcat(char result[], const char string1[],
const char string2[])
{
int i, j;
i = 0;
while(string1[i] != '\0'){
result[i] = string1[i];
i++;
}
for (j=0; string2[j] != '\0'; j++){
result[i+j] = string2[j];
}
result[i+j] = '\0';
}

27
string equality
bool equalString(const char string1[], const char
string2[])
{
int i = 0;
while ((string1[i] != '\0') &&
(string2[i] != '\0') &&
(string1[i] == string2[i]))
++i;
if (string1[i] == '\0' && string2[i] == '\0')
return true;
else
return false;
}
28
Exercise

 Write a function upperCase that receives


one string and changes it to have upper
case letters only
 if the letter is lower case, then substract ‘a’-’A’
from it
 otherwise don’t modify it

29
Exercise - Solution

30

You might also like