0% found this document useful (0 votes)
9 views22 pages

CLang Lect12

The document discusses strings in C programming. It covers how strings are stored in memory as arrays of characters with a null terminator. It provides examples of declaring and initializing string variables. It also discusses common string operations like copying, concatenating, comparing strings using library functions like strcpy(), strcat(), and strcmp(). It highlights common mistakes when working with strings and passing strings as function parameters. Finally, it provides examples of functions that operate on strings like capitalizing letters or extracting a name from a full name string.

Uploaded by

Thành
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)
9 views22 pages

CLang Lect12

The document discusses strings in C programming. It covers how strings are stored in memory as arrays of characters with a null terminator. It provides examples of declaring and initializing string variables. It also discusses common string operations like copying, concatenating, comparing strings using library functions like strcpy(), strcat(), and strcmp(). It highlights common mistakes when working with strings and passing strings as function parameters. Finally, it provides examples of functions that operate on strings like capitalizing letters or extracting a name from a full name string.

Uploaded by

Thành
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/ 22

String

Department of Information System


SoICT, HUST

1
Store in the memory
• Remind:
– Each cell in the memory is addressed.
– Each variable declaration takes one cell to
store value.

Example: char ch;

ch = ‘B’;

0x1FFE 0x1FFF 0x2000 0x2001 0x2002

‘B’ etc
ch
2
Representing a string
• A character string is a char array
• Each cell in the array contains a char
• The character string must have the terminating
character (’\0’), aka null character. The address of
the first character is the string’s address

Example: char name[5];

name
= 0x2000

0x2000 0x2004

3
String declaration
Declare 1:
char name[5] = “Ann”; Terminating character
(NULL character)

name A n n \0
= 0x2000

0x2000 0x2004

Equal declare:
char name[5] = {’A’,’n’,’n’,’\0’};

4
String declaration
Declare 2: An additional character
char name[] = “Ann”; for end of string ‘\0’

name A n n \0
= 0x2000
0x2000 0x2003
Attention:
char name[] = ‘Ann’; No character for end
of string (‘\0’)

name A n n
= 0x2000
0x2000 0x2002
5
String declaration

Declaration 3:
char *name = “Ann”;

0x3000 A n n \0
name

0x3000 0x3003
Can change
reference for the
pointer name

6
Input/output a string
#include <stdio.h> Declare a
constant
#define MAXLENGTH 15

int main()
{
char str1[MAXLENGTH];
char str2[MAXLENGTH]; No operant &
here

scanf("%s", str1); gets() allows input


gets(str2); a string with spaces
printf("%s\n%s\n", str1, str2);

return 0;
}
7
Character in string
0x3995 0x399C

name J o h n \0
= 0x3995

index 0 index 2

char name[8] = “John”;


int i = 2;

printf(“Char at index %d is %c.\n”, i, name[i]);

output: Char at index 2 is h.

8
Program to calculating the number of
character
• Calculate the characters that are not spaces in the input string

#include <stdio.h>

int main()
{
char str[80];
int dem, i;

printf("Nhap xau bat ki: ");


gets(str);

dem = 0; i = 0;
while ( str[i] != '\0' ) {
if ( str[i] != ' ' ) dem++;
i++;
}
printf(“So ki tu khac trang trong xau la %d", dem);

return 0;
}
9
String operations
• #include <string.h>
• Use library functions declared in <string.h>
• Assignment : strcpy()
char s1[25];
char s2[25];
strcpy(s1, “Hello”);
strcpy(s2, s1);

10
String assignment
#include <stdio.h>
#include <string.h>

#define MAXLENGTH 100

int main()
{
char string1[MAXLENGTH];
char string2[MAXLENGTH];

strcpy(string1, “Hello World!”);


strcpy(string2, string1);

return 0;
}

string1: “Hello World!”


string2: “Hello World!”
11
Common mistakes
s1 = “Hello”;
s2 = s1;
s1 = s1 + “Anna”;
s2 = s2 + “World”;

char s[4];
strcpy(s, “Hello”);

12
String operations
• Concatenation : strcat()
strcat(s1, “ Anna”);
strcat(s2, “ World”);
• Memory leak maybe occurs when
copying/concatenating strings.
• Common errors:
char name[5];
strcpy(name, “Ann”);
strcat(name, “ Smith”);

13
String concatenation

char string1[80];
char string2[80];

strcpy(string1, “Goodbye”);
strcpy(string2, “, Cruel ”);

strcat(string1, string2);
strcat(string1, string2);
strcat(string1, “World!”);

string1: “Goodbye, Cruel , Cruel World!”


string2: “, Cruel ”

14
String operations
• Comparison : strcmp()
• strcmp returns 0 if str1 = str2
<0 if str1 < str2
>0 if str1 > str2
char str1[] = “Windows”;
char str2[] = “Unix”;
if (strcmp(str1, str2) < 0)
printf(“%s %s\n”, str1, str2);
else printf(“%s %s\n”, str2, str1);

15
Common errors

strcpy(string1, “Apple”); Compare


strcpy(string2, “Wax”); addresses of the
two strings
if (string1 < string2)
{
printf(“%s %s\n”, string1, string2);
}
else
{
printf(“%s %s\n”, string2, string1);
}

16
Strings as function parameters
• Declare as char* or char[]
void greeting (char* name)
void greeting (char name[])
• It points to the first character of the string
• Changes to the string inside the function affect the
actual string
• It is not necessary to pass the length of the string to
the function

17
Example

char *capitalize(char * str)


{
for (i=0; i<strlen(str); i++)
if ( str[i]>=’a’&& str[i]<=’z’)&&
(i==0||str[i-1]==’ ’) )
str[i] = ’A’ + (str[i]-’a’);
return str;
}

18
Example
• Write a function that returns the name from a full name. The full name is not
an empty string and does not have extra spaces

char * timten(const char[] hoten)


{
int i;
i = strlen(hoten)-1;
/* Find the last space in the string */
while (i >= 0 && hoten[i] != ' ') i--;
return hoten + i + 1;
}

hoten+i+1

hoten T r a n T i e n \0

Index of the space


(i=4)
19
Example (con’t)

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

char * timten(const char[] hoten);

int main()
{
char hoten[80];

printf("Nhap mot xau ho va ten: ");


gets(hoten);

printf("Ten sau khi tach duoc: %s", timten(hoten));

return 0;
}
20
Exercises
(i) Trim left blanks, right blanks, and redundant
blanks in a string.
(ii) Inverse a string.
(iii) Copy first name or last name in a full name string.

21
Thank you
for your
attentions!

You might also like