Home | About | Comments | Lectures | Notice | Registration
C Programming
Main Page
Review
Lesson Four (a)
Lesson Five
This lesson will introduce the use of handling strings with the scanf( ) function.
The scanf( ) function does not handle string data as smoothly as it does numeric data.
You Will Learn :
The function strcmp() is used to compare two strings.
The function strcmp( ) requires two arguments. The second argument called second_word in our example is compared to the first argument called first_word. If the two arguments are the same the function returns a zero value. The general format is as follows :
strcmp(first_word, second_word)
If strcmp( ) returns 0 then the words are the same.
#include<stdio.h> #include<conio.h> #include<string.h> void main() { char word1[35], word2[35]; clrscr(); printf("Enter a word : "); gets(word1); printf("\nEnter another word to compare :); gets(word2); if(strcmp(word1,word2) ==0) printf("\nThe words are the same"); else printf("\nThe words are different"); exit(0); } |
Instructions :
There is a second compare function that we can use that ignores the case of the strings being compared and it is stricmp( ). Replace the strcmp() function with the new stricmp() and test the program mixing upper and lower case characters.
Modify your source code as follows :
<
#include<stdio.h> #include<conio.h> #include<string.h> void main() { char word1[35], word2[35]; clrscr(); printf("Enter a word : "); gets(word1); printf("\nEnter another word to compare :); gets(word2); if(stricmp(word1,word2) ==0) printf("\nThe words are the same"); else printf("\nThe words are different"); exit(0); } |
The function strncmp( ) allows us to compare selected characters from a string. The statement strncmp(accountnum,"01",2) == 0 allows us to compare the first two (2) characters form a variable accountnum against the value 01
Remember the symbols != means not equal and == means equal to.
If the values compared are the same then the function returns the number 0 . Which means that the result is true.
Enter the following source code, compile and debug code before saving.
#include<stdio.h> #include<conio.h> #include<string.h> void main() { char accountnum[35]; clrscr(); printf("\t \t Total Savings Bank "); printf("\n Enter a four digit account number - "); printf("\n Start with 01 for Savings Account or 02 for Checking Account"); gets(accountnum); if (strncmp(accountnum,"01",2) ==0) printf("This is a savings account"); if (strncmp(accountnum,"02",2) ==0) printf("This is a cheque account"); if (strncmp(accountnum,"01",2 !=0) || (strncmp(accountnum,"02",2 !=0))) printf("Invalid Account Number"); exit(0); } |
This code allows the user to enter a four digit account number that should begin with 01 or 02. If you test the program with numbers other than those advised then an Invalid Account message is displayed.
Test the program with the following input values and record the output
Pro4f.c accepts a name from the user, adds a space then places the user’s surname at the end of the string. Finally the new string is displayed on screen.
Enter the source code exactly as written before experimenting with it..
#include<stdio.h> #include<conio.h> #include<string.h> void main() { char name1[35], name2[35]; clrscr( ); printf("Enter your first name : "); gets(name1); printf(“Enter your surname ”); gets(name2); /* add a space to the end of your first name string */ strcat(name1," "); /* strings are concaternated */ strcat(name1,name2); exit(0); } |
The Tutor |
|
Home | About | Comments | Lectures | Notice | Registration