Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

A C Program for Comparing two string using user defined function, Without using string.h function : Wether they are equal or not

Leave a Comment
/* The following program is case sensitive, & let the user to enter 2 strings. After entering the 2 string the program check, character by character if any of the character is not equal it will generate the message of inequality otherwise they are equal. */

//CODE:
#include <stdio.h>
#include <stdlib.h>
int ostrcmp(char *,char *);

int main(void)
{
    char a[100], b[100];
    printf("NOTE: The program is case sensitive\n\n");
    printf("Enter the string 1:\n");
    gets(a);
    printf("Enter the string 2:\n");
    gets(b); 

        if (ostrcmp(a, b)) printf("Strings match.\n");
        else printf("Strings don't match.\n");
        system("pause");
        return 0;
}

int ostrcmp(char *str1, char *str2) {
        while (*str1 != '\0') {
                if (*str1 != *str2) return 0;
                str1++;
                str2++;
        }
        if (*str2 != '\0') return -1;
        return 1;
}


Screenshot:
A C Program for Comparing two string using user defined function, Without using string.h function : Wether they are equal or not
A C Program for Comparing two string using user defined function, Without using string.h function : Wether they are equal or not

Read More...

C Program Code for finding the string length without using string.h library function strlen, custom made strlen

Leave a Comment
 /* The following C Program Code contains a custom made strlen function, without using string.h it returns the length of the string. The user is prompted to enter the string of which the length is to be found. */

#include<stdio.h>
#include<conio.h>

int newstrlen(char*src)
//Function for string Length
{
int c=0;
while(*src!='\0')
{
++c;
src++;
}
return c;
}

int main()
{
  char s[100];
  int l;
  printf("\nEnter a string: ");
  gets(s);
  l=newstrlen(s);
  printf("\nString lenght is:%d",l);
  getch();
  return 0;
}


Screenshot:
C Program Code for finding the string length without using string.h library function strlen, custom made strlen

Read More...

C Program Code For Copying the String Without STRCPY Function, Custom Made StringCopy Function

1 comment
 /* The following C  program is used to copy a string in another array without using string.h function, strcpy(). The program asks for the input from user & the value is copied in the 2nd variable then displayed to user. */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void mystrcpy(char *d, char *s) 
//Function definition for string copy function
{
while(*s)
//While the source string is not encountered a NULL character
{
 *d=*s;
 s++;
 d++;
}
*d='\0';
}


int main()
{
  char s1[100];
  char s2[100];
  printf("\nEnter a string to copy: ");
  gets(s2);  
//User Input the string here
  mystrcpy(s1,s2);
  printf("\nThe Copied String is : ");
  puts(s2); 
//Copied string output shown here
  system("pause");
  return 0;
}


Screenshot:
C  Program Code For Copying the String Without STRCPY Function, Custom Made StringCopy Function

Read More...