Lecture 7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Object Oriented Programming –

CS/CE 224/272

Nadia Nasir

Habib University, Fall 2024


Character Arrays
(C String)
Strings are Character Arrays

Strings in C are simply arrays of characters.


Example: char s [10];
This is a ten (10) element array that can hold a character
string consisting of  9 characters.
This is because C does not know where the end of an
array is at run time.
By convention, C uses a NULL character '\0' to terminate all
strings in its library functions
For example:
char str [10] = {'u', 'n', 'I', 'x', '\0'};
It’s the string terminator (not the size of the array) that
determines the length of the string.
Accessing Individual Characters

The first element of any array in C is at index 0. The second is at index 1, and so on ...
char s[10];
s[0] = 'h';
s[1] = 'i’;
s[2] = '!'; h i ! \0 ? ? ? ? ? ?
s[3] = '\0';
s in[0]
This notation can be used [1] of
all kinds [2]statements
[3] [4] and
[5]expressions
[6] [7] [8] in C: [9]
For example:
c = s[1];
if (s[0] == '-') …
switch (s[1]) ...
String Literals

String literals are given as a string quoted by


double quotes.
cout << "Long long ago.";
Initializing char array ...
char s[10]="unix"; /* s[4] is '\0'; */
char s[ ]="unix"; /* s has five elements */
Inputting Strings with getline( )
cin.getline( ) gets a line from the standard input.

char your_line[100];
cout << "Enter a line:\n";
cin.getline (your_line,100);
You can overflow your string buffer, so be careful!
The C String Library

String functions are provided in a standard


string library.
Access this through the include file:
#include <cstring>
Includes functions such as:
Computing length of string
Copying strings
Concatenating strings
strlen

strlen returns the length of a NULL terminated character


string:
size_t strlen (char * str) ;
size_t
A type defined in string.h that is equivalent to an unsigned
int
char *str
Points to a series of characters or is a character array ending
with '\0'
The following code has a problem!
char a[6]={‘a’, ’b’, ’c’, ’d’, ’e’ ,’\0’};
strlen(a); //5
strcpy
Copying a string comes in the form:
char *strcpy (char * destination, char * source);
A copy of source is made at destination
source should be NULL terminated
destination should have enough room
(its length should be at least the size of source)
The return value also points at the destination.
strcat

Included in cstring and comes in the form:


char * strcat (char * str1, char * str2);
Appends a copy of str2 to the end of str1
A pointer equal to str1 is returned
Ensure that str1 has sufficient space for the
concatenated string!
Array index out of range will be the most
popular bug .
Comparing Strings

C strings can be compared for equality or


inequality
If they are equal - they are ASCII identical
If they are unequal the comparison function
will return an int that is interpreted as:
< 0 : str1 is less than str2
0 : str1 is equal to str2
> 0 : str1 is greater than str2
strcmp

Four basic comparison functions:


int strcmp (char *str1, char *str2) ;
Does an ASCII comparison one char at a time
until a difference is found between two chars
Return value is as stated before
If both strings reach a '\0' at the same time, they
are considered equal.
int strncmp (char *str1, char * str2, size_t n);
Compares n chars of str1 and str2
Continues until n chars are compared or
The end of str1or str2 is encountered
Also have strcasecmp() and strncasecmp() which do
the same as above, but ignore case in letters.
Example

An Example - strncmp

#include<iostream>
#include<cstring>
using namespace std;
int main() {
char str1[] = "The first string.";
char str2[] = "The second string.";
size_t n, x;
cout << strncmp(str1,str2,4) ; // 0
cout << strncmp(str1,str2,5) ; // -1
}
Searching Strings (1)
There are a number of searching functions:
char * strchr (char * str, int ch) ;
strchr search str until ch is found or NULL character is
found instead.
If found, a (non-NULL) pointer to ch is returned.
Otherwise, NULL is returned instead.
You can determine its location (index) in the string by:
Subtracting the value returned from the address of
the start of the string
More pointer arithmetic … more on this later!
Example

Example use of strchr:


#include<iostream>
#include<cstring>
using namespace std;
int main() {
char ch='b', buf[80];
strcpy(buf, "The quick brown fox");
if (strchr(buf,ch) == NULL)
cout << "The character " << ch << "was not found.\n" ;
else
cout << "The character " << ch << " was found at position " <<
strchr(buf,ch)-buf+1 <<endl;
}
Converting Strings to Numbers (1)
int atoi (char *ptr);
Takes a character string and converts it to an integer.
White space and + or - are OK.
Starts at beginning and continues until something non-
convertible is encountered.
Some examples:
String Value returned
"157" 157
"-1.6" -1
"+50x" 50
"twelve" 0
"x506" 0
Converting Strings to Numbers (2)

long atol (char *ptr) ;


Same as atoi except it returns a long.
double atof (char * str);
Handles digits 0-9.
A decimal point.
An exponent indicator (e or E).
If no characters are convertible a 0 is returned.
Examples:
String Value returned
"12" 12.000000
"-0.123" -0.123000
"123E+3" 123000.000000
"123.1e-5" 0.001231

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy