0% found this document useful (0 votes)
38 views13 pages

Ics 2102 Strings

Uploaded by

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

Ics 2102 Strings

Uploaded by

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

ICS 2102: INTRODUCTION

TO COMPUTER
PROGRAMMING.
C STRINGS.

Strings are used for storing text/ characters.


Example:
“I love programming” is a string of characters.
C does not have a string type to create variables. Instead you must use the char type and create an array, of characters to
make a string in C:
char greetings[] = “Hello world”;
Note the use of double quotes.
To out put the string, you can use the printf() function together with the format specifier %s.
char greetings[] = “Hi ”;
printf(“%s”, greetings);
ACCESS STRINGS.

Since strings are actually arrays, you can access a string by referring to its index number inside square
brackets[].

This example prints the first character 0 in greetings;

char greetings [] = “Hello world”;


printf(“%c”, greetings[0]);
//prints H

Note: we use %c format specifier to print a single character.


MODIFY STRINGS.

To change the value of a specific character in a string, refer to the index number, and use single quotes.

char greetings[] = “Hello world”;


greetings [0]= ‘J’;
printf(“%s”, greetings);
//outputs Jello world
LOOP THROUGH A STRING.

You can also loop through the characters of a string, using a for loop:
char carName[]= “volvo”;
int i;
for(i=0,i<5;i++){
printf(“%c\n”, carName[i]);
}
Let’s use the sizeof() formula instead of manually writing the size of an array in the loop condition (i < 5)
to make the loop more ideal;
char carName[]= “volvo”;
int length = sizeof(carName)/sizeof([0]);
int i;
for(i=0,i< length;i++){
printf(“%c\n”, carName[i]);
}
Another way of creating strings.

So far we have used a ‘string lateral’ to create a string variable, which is the easiest way to create a string in C.
You can also create a string with a set of characters.
Example:
Char greetings[] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘w’, ‘o’, ‘r’, ‘l’, ‘d’ ‘\o’};
Printf(“%s”, greetings);
Note: we include \o character at the end.
This is the null terminating character. It tells C that this is the end of the string.
C SPECIAL CHARACTERS.

When we want to use special characters like single quotes, double quotes and backslash within strings we use the
backslash escape character(\).
Example:
Char txt[]= “We are \”seniors\” here.”;
//outputs we are “seniors” here
Char txt[] = “It\’s okay”;
//outputs It’s okay
The backslash escape character turns special characters into string characters.
Other popular escape characters are;
\n – new line
\t – tab
\o – null
C STRING FUNCTIONS.

C also has many useful string functions, which can be used to perform certain operations on strings.
To use them, you must include the <string.h> header file in your program.
#include <string.h>

STRING LENGTH.
To get the length of a string, you can use the strlen( function):

char text[] = “hi”;


printf(“%d”, strlen(text)); //output 2
strlen() and sizeof() behave differently. Since sizeof() includes the \o character when counting.
Sizeof() usually returns the memory size in bytes and not the actual string length.
STRING CONCATENATION

To concatenate two strings means to combine. We use strcat() function.


Char str1[20] = “Hello”;
Char str2[] = “World”;

Strcat(str1, str2);
//concatenate str2 to str1 result is stored in str1

Printf”%s”, str1);
Note: the size of str1 should be large enough to store the result of both strings combined.
COPY STRINGS

To copy the value of one string to another, you can use the strcpy() function.

Char str1[20]= “hello world”;


Char str2[20] ;

//copy str1 to str2


Strcpy(str2, str1);

Printf(“%s”, str2);

Note: the size of str2 should be large enough to store the copied string.
COMPARE STRINGS

To compare two strings, you can use the strcmp() function.


It returns 0 if the two strings are equal, otherwise a value that is not 0.
Char str1[] = “Hello”;
Char str2[] = “Hello”;
Char str3[] = “Hi”;

//compare str1 and str2, and print the result


Printf(“%d\n”, strcmp(str1, str2));
//returns 0 the strings are equal.

You might also like