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

PROGRAMMING Code Panel

FINALS REVIEWER – STRING FUNCTIONS & FILE HANDLING

What is a STRING?
- A.K.A an Array of Characters
A character is a variable that is alone.
This is a declaration of a character. char Alpha = ‘a’;

If there’s a group of characters, we call it an “Array of


Characters or the “String”.
char Alpha[10] = “abcd”;
These are the possible declarations of an Array of Character
char Alpha[10] = {‘a’, ‘b’, ‘c’, ‘d’};
How can we access or use this String? 0 1 2 3

With the use of what we call “Array Index number”.


In the String Alpha here are its index

Look at the declaration of String Alpha there is a square


bracket with a number inside, it is called “Array Size”.
Array size means of “How many characters will/should I char Alpha[4] = {‘a’, ‘b’, ‘c’, ‘d’};
store?”, We can see in String Alpha it is size of 10 but there
are only 4 characters in it which is the {‘a’, ‘b’, ‘c’, ‘d’}. So,
we can change it to size of 4. The number of values between braces { } can not be larger than
the number of elements that we declare for the array between
In conclusion, remember the last index number of a
square brackets [ ]
Character Array is Array size minus one(1), 4 -1 =3;
WAYS OF PRINTING/ACCESSING STRINGS Code Panel

With for loop(Hard way but useful in tracing)


char Alpha[4] = {‘a’, ‘b’, ‘c’, ‘d’};
Let's use the String Alpha again.
0 1 2 3
Keep in mind that we can access each
character in String Alpha by using its indexes, printf("%c", Alpha[0]);
Here’s how.

So, if we want to print everything inside String This will print character ‘a’ only, because index
Alpha, we should use a loop if it too long. 0 is a. Remember, Index not Size.

Rather than using this WRONG way. printf("%c", Alpha[0]);


printf("%c", Alpha[1]);
printf("%c", Alpha[2]);
printf("%c", Alpha[3]);

Use this.
for (int i = 0; i <= 3; i++)
{
To practice tracing, you can play with the loop.
printf("%c", Alpha[i]);
Like adding increments and decrements.
}
Example:
char Alpha[5] = “abcdm”;
for (int i = 0; i <= 4; i+=2) {
printf("%c", Alpha[i]);
}

What do you think is the output? Don’t Cheat!


WAYS OF PRINTING/ACCESSING STRINGS Code Panel

char Alpha[5] = “abcdm”;


With printf(“%s”, _____);

Reminder this will print all what’s inside the Sring variable printf("%s", Alpha);

This way works better in formatting with escape symbols like


\n and \t

Data Specifier for String

With puts(____);

Reminder this will print all what’s inside the Sring variable
Uses conio.h header puts(Alpha);

These are just the basic uses and techniques; you can learn
more or even make your own way.
THE STRING FUNCTIONS Code Panel
There are numerous string functions out there but here are
int x;
the list of commonly used ones:
char Alpha[7] = {'a', 'b', 'c', 'd'};

STRING LENGTH printf("%s", Alpha);


x = strlen(Alpha);
1. strlen(string_name); printf("\t%d", x);
- returns the length of the string(it returns an integer)
- Reminder string length is different from string size
This holds the integer value that strlen will give.
String Length is the actual numbers of characters inside the
array.

String Size is the number capacity of the array to and can hold.

Look at the code panel.

char Alpha[7] = {'a', 'b', 'c', 'd'};

As you observe, it only gives the number of characters


inside the array. Which means index 4, 5, 6 is empty or in
programming means NULL.
THE STRING FUNCTIONS Code Panel

STRING UPPER char Alpha[7] = {'a', 'b', 'c', 'd'};

2. strupr(string_name); printf("Original: %s\n", Alpha);


- convert to uppercase strupr(Alpha);
printf("With Upper: %s", Alpha);

This is different from the toupper function in ctype.h, because


toupper only converts PER CHARACTER;

While strupr converts group of characters or a string.

Look at the code panel.


THE STRING FUNCTIONS Code Panel

STRING LOWER char Alpha[7] = {‘A', ‘B', ‘C', ‘D'};

3. strlwr(string_name); printf("Original: %s\n", Alpha);


- convert to lowercase strlwr(Alpha);
printf("With Lower: %s", Alpha);

This is different from the tolower function in ctype.h, because


tolower only converts PER CHARACTER;

While strlwr converts group of characters or a string.

Look at the code panel.


THE STRING FUNCTIONS Code Panel

STRING REVERSE char Alpha[7] = {‘A', ‘B', ‘C', ‘D'};

4. strrev(string_name); printf("Original: %s\n", Alpha);


- reverses the order of string strrev(Alpha);
printf(“Reversed: %s", Alpha);
Look at the code panel.

Which means it also reverses the order of indexes

At first ‘A’ is index 0


After reversing, it is now index 3
THE STRING FUNCTIONS Code Panel

STRING COPY char Alpha[7] = "ABCD";


char Bravo[7] = "EFGH";
5. strcpy(string_1, string_2);
- copies the string form one variable to another
printf("Original ALPHA:\t %s\n", Alpha);
Look at the code panel.
printf("Original BRAVO:\t %s\n", Bravo);

strcpy(Bravo, Alpha);

printf("NEW BRAVO:\t %s", Bravo);

strcpy(Bravo, Alpha);

Where to put the copy


What to copy
THE STRING FUNCTIONS Code Panel

STRING NUMBER COPY char Alpha[7] = "ABCD";


char Bravo[7] = "EFGH";
6. strncpy(string_1, string_2, number);
- copies the string form one variable to another,
desired length of characters printf("Original ALPHA:\t %s\n", Alpha);

Look at the code panel. How long or how many printf("Original BRAVO:\t %s\n", Bravo);
character to copy, start
from 1st index strncpy(Bravo, Alpha, 2);

printf("NEW BRAVO:\t %s", Bravo);

strcpy(Bravo, Alpha, 2);

Where to put the copy


What to copy

The first 2 character of Alpha is ‘A’ and ‘B’ so that’s the only
thing will be copied to Bravo so the 1st 2 of bravo will be
overwritten
THE STRING FUNCTIONS Code Panel

SAME
int x;
STRING COMPARE char Alpha[7] = "ABCD";
char Bravo[7] = "ABCD";
7. strcmp(string_1, string_2);
- Compares 2 strings. This is case sensitive.
printf("ALPHA:\t %s\n", Alpha);
Look at the code panel. printf("BRAVO:\t %s\n", Bravo);
x = strcmp(Bravo, Alpha);
If the 2 strings are exactly the same also the cases are same, it printf("Compare value:\t %d\n", x);
will return 0. Which means nothing is different.

DIFFERENT
While, if 2 string have difference it will return 1.
int x;
char Alpha[7] = "ABCD";
Remember :
char Bravo[7] = "abcd";
ABCD is different from
printf("ALPHA:\t %s\n", Alpha);
Abcd
printf("BRAVO:\t %s\n", Bravo);
aBcd
x = strcmp(Bravo, Alpha);
abCd
printf("Compare value:\t %d\n", x);
abcD

In this function.
THE STRING FUNCTIONS Code Panel

SAME
int x;
STRING INSENSITIVE COMPARE char Alpha[7] = "ABCD";
char Bravo[7] = "abcd";
8. stricmp(string_1, string_2);
- Compares 2 strings. This is case sensitive.
printf("ALPHA:\t %s\n", Alpha);
Look at the code panel. printf("BRAVO:\t %s\n", Bravo);
x = stricmp(Bravo, Alpha);
If the 2 strings are the same regardless of the case, it will return printf("Compare value:\t %d\n", x);
0. Which means nothing is different.

DIFFERENT
While, if 2 string have difference it will return nonzero value.
int x;
char Alpha[7] = "ABCD";
Remember :
char Bravo[7] = “Tite";
ABCD is same from
printf("ALPHA:\t %s\n", Alpha);
Abcd
printf("BRAVO:\t %s\n", Bravo);
aBcd
x = stricmp(Bravo, Alpha);
abCd
printf("Compare value:\t %d\n", x);
abcD

In this function.
THE STRING FUNCTIONS Code Panel

char Alpha[7] = "MALA";


char Bravo[7] = "KI";
STRING CONCATENATE

9. strcat(string_1, string_2); printf("ALPHA:\t %s\n", Alpha);


- connects string 2 to string 1. printf("BRAVO:\t %s\n", Bravo);
string 1 + string 2 strcat(Alpha, Bravo);
printf("New Alpha value: %s\n", Alpha);
Look at the code panel.

MALA+KI = MALAKI

Remember the value of string bravo is still KI. Only


Alpha or the 1st string will be modified
THE STRING FUNCTIONS Code Panel

char Alpha[7] = "MALA";


char Bravo[7] = "KINA";
STRING NUMBER CONCATENATE

10. strncat(string_1, string_2, Num); printf("ALPHA:\t %s\n", Alpha);


- connects string 2 to string 1. printf("BRAVO:\t %s\n", Bravo);
string 1 + string 2 strncat(Alpha, Bravo, 3);
printf("New Alpha value: %s\n", Alpha);
Look at the code panel.

MALA+KIN = MALAKIN

Remember the value of string bravo is still KI. Only


Alpha or the 1st string will be modified
What to connect

strncat(Alpha, Bravo, 3);

Where to connect

How many characters to


connect
THE FILE HANDLING Code Panel

The basic Modes of File Handling

1. “w” – Write Mode


It creates a file in case of inexistence and overwrite or clear
file in case of existence of the file. FILE *wantu;
2. “a” – Append Mode
It adds data to the file. If you want to add the data to the wantu = fopen("wantu.txt", "w")
file, it will be inserted at the end ;
of the file. If a file does not exist, it will automatically be wantu = fopen("wantu.txt", “a");
created
3. “r” – Read Mode wantu = fopen("wantu.txt", “r");
Open an existing file for processing, basically reading whats
inside the file. fprintf(wantu, "%s", "Hello");

Essential Functions in File Handling fclose(wantu);

fopen(); - File open, opens the file


fclose(); - File close, closes the file
fprintf(); prints data into the text file
fscanf(); scans data from the text file
THE FILE HANDLING Code Panel
Writing and Append

Writing and append has almost the same syntax or format The file name of your txt
FILE pointer

File mode
1st You declare a FILE pointer
2nd open a file with fopen FILE *wantu;
3rd you can now fprintf to the file
4th close the file using fclose wantu = fopen("wantu.txt", "w");

fprintf(wantu, "%s", "Hello");

fclose(wantu);
File pointer

fprintf is fundamentally the same as printf. In fprintf you


are just printing on to the file. Data Specifier

Your variable or what you


want to write or add to your
file
THE FILE HANDLING Code Panel
Read

Writing and append has almost the same syntax or format The file name of your txt
FILE pointer

File mode
1st You declare a FILE pointer
2nd open a file with fopen FILE *wantu;
3rd you can now fscanf from the file
4th close the file using fclose wantu = fopen("wantu.txt", “r");

fscanf(wantu, "%s", name);

fclose(wantu);
File pointer

fscanf is fundamentally the same as scanf. In fscanf you are


just scanning from the file. Data Specifier

Remember
The variable that will hold
what will you read from the
In multiple data reading: file
Be cautious of the arrangement of you Data specifier. It
should be equivalent of how you placed them in your file.
THE FILE HANDLING Code Panel
Multiple Read

Writing and append has almost the same syntax or format The file name of your txt
FILE pointer

File mode
1st You declare a FILE pointer
2nd open a file with fopen FILE *wantu;
3rd you can now fscanf from the file
4th close the file using fclose wantu = fopen("wantu.txt", “r");

while(fscanf(wantu, "%s", name) != EOF){

fclose(wantu);

This just means that while it is not yet in the END OF FILE
it will still resume reading

After reading you can now printf the variable where you
stored the value in reads.

printf(“%s”, name);

You might also like