Professional Documents
Culture Documents
Prog Finals Reviewer-V1
Prog Finals Reviewer-V1
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’;
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.
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]);
}
Reminder this will print all what’s inside the Sring variable printf("%s", Alpha);
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 Size is the number capacity of the array to and can hold.
strcpy(Bravo, Alpha);
strcpy(Bravo, 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);
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
MALA+KI = MALAKI
MALA+KIN = MALAKIN
Where to connect
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");
fclose(wantu);
File pointer
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");
fclose(wantu);
File pointer
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");
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);