1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- /*
- * Copyright (C) 2020, 2019, 2018, 2017 Girish M
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- *
- */
- /*---------------------------------------------------------------------------
- Name: Girish M
- Roll number: cs1713
- Date: 1 August 2017
- Program description: Let s and t be strings containing at most 100 characters.
- Implement the following functions in C:
- (a) strlen(s): returns the length of s, i.e., the number of characters
- present in s;
- (b) strcmp(s, t): returns 1 if s and t are identical, 0 otherwise;
- (c) diffByOne(s, t): returns 1 if s and t are of the same length, and
- differ in exactly one position, 0 otherwise.
- Acknowledgements:
- ---------------------------------------------------------------------------*/
- #include <stdio.h>
- int strlength(char* str)
- {
- int len=0;
- while(*str++ != '\0')
- len++;
- return len;
- }
- int strcompare(char* s, char* t)
- {
- while(*s != '\0')
- {
- if(*s != *t)
- {
- return 0;
- }
- s++;
- t++;
- }
- return 1;
- }
- int diffByOne(char* s, char* t)
- {
- int numDiff=0;
- if(strlength(s) == strlength(t))
- {
- while(*s != '\0')
- {
- if(*s != *t)
- {
- numDiff++;
- }
- s++;
- t++;
- }
- if(numDiff == 1)
- return 1;
- else
- return 0;
- }
- else
- return 0;
- }
- int main(int argc, char* argv[])
- {
- //const int size_str = 100;
- if(argc == 3)
- {
- printf("\nstrlen(%s) is %d\n", argv[1], strlength(argv[1]));
- printf("\nstrlen(%s) is %d\n", argv[2], strlength(argv[2]));
- printf("\nstrcmp(%s, %s): %d\n", argv[1], argv[2], strcompare(argv[1], argv[2]));
- printf("\ndiffByOne(%s, %s): %d\n", argv[1], argv[2], diffByOne(argv[1], argv[2]));
- }
- else
- printf("\nUsage: ./cs1713-day3-prog1.o str1 str2\n");
- return 0;
- }
|