/*This function will chechk whether the input is an alphabate character or not.*/
int myisalpha(int x)
{
if((x >= 65 && x <=90) || (x >= 97 && x <= 122))
return 1;
else
return 0;
}
Tuesday, 11 December 2012
Implement strstr
/*This program will find the substring s2 from the string s1
* and if found the it reutrns the base address of the substring
* if not found it send zero as a character pointer.
* */
char *mystrstr(const char *string, const char *needle)
{
const char *s1;//For retaing address of second sring for checking
const char *tar; //For retaing address of first string
/*Loop untill end*/
for(; *string != '\0'; string++){
for(s1 = string, tar = needle; *tar != '\0'; s1++, tar++){
if(*s1 != *tar)
break;
}
if(*tar == '\0')
return (char *)s1;
}
return NULL;
}
Implement strcasecmp
/*This function will comapre two string without cases and returns 0 if they are same or the difference of the ascii value if they are difference*/
int mystrcasecmp(const char *s1, const char *s2)
{
while(*s1!='\0' && *s2!='\0'){
if(toupper(*s1) && toupper(*s2)){
s1++;
s2++;
}
else
return(*s1-*s2);
}
return (*s1 - *s2);
}
int mystrcasecmp(const char *s1, const char *s2)
{
while(*s1!='\0' && *s2!='\0'){
if(toupper(*s1) && toupper(*s2)){
s1++;
s2++;
}
else
return(*s1-*s2);
}
return (*s1 - *s2);
}
Inplement strtok
/*It will tokenize the source string "src" from the place where any of the charcter of the
* target string "tar" will be found. It returns the stating address the token string and
* it should catch by a char pointer
* */
char *mystrtok(char *str, const char *tar)
{
static int i; /*For looping*/
static int len1; /*Holds the length of source string*/
static int len2; /*Holds the length of delimiter string*/
static char *s; /*This variable will holds the address of source string*/
int j ; /*For looping*/
int ptr = 0 ; /*Using for recognizing consecutive delimeter found*/
char *token; /*Variable for storing the token*/
token=(char *)malloc(25*sizeof(char)); /*Creating memory for token*/
if(token == NULL) {
printf("Out of memory space!");
exit(0);
}
/* Defining constraints for first time only*/
if(str != '\0') {
i = 0;
s = str;
len1 = strlen(str);
len2 = strlen(tar);
}
i++;
token=(s + i - 1);
/*tokenizing untill last*/
while(i <= len1+1){
ptr++;
for(j = 0 ; j <= len2 ; j++){
if(*(s + i - 1) == *(tar + j)){
*(token + ptr - 1) = '\0';
if(*token == '\0'){
token++;
ptr = 0;
break;
}
else
return token;
}
if(ptr == 0)
continue;
}
i++;
}
return '\0';
}
* target string "tar" will be found. It returns the stating address the token string and
* it should catch by a char pointer
* */
char *mystrtok(char *str, const char *tar)
{
static int i; /*For looping*/
static int len1; /*Holds the length of source string*/
static int len2; /*Holds the length of delimiter string*/
static char *s; /*This variable will holds the address of source string*/
int j ; /*For looping*/
int ptr = 0 ; /*Using for recognizing consecutive delimeter found*/
char *token; /*Variable for storing the token*/
token=(char *)malloc(25*sizeof(char)); /*Creating memory for token*/
if(token == NULL) {
printf("Out of memory space!");
exit(0);
}
/* Defining constraints for first time only*/
if(str != '\0') {
i = 0;
s = str;
len1 = strlen(str);
len2 = strlen(tar);
}
i++;
token=(s + i - 1);
/*tokenizing untill last*/
while(i <= len1+1){
ptr++;
for(j = 0 ; j <= len2 ; j++){
if(*(s + i - 1) == *(tar + j)){
*(token + ptr - 1) = '\0';
if(*token == '\0'){
token++;
ptr = 0;
break;
}
else
return token;
}
if(ptr == 0)
continue;
}
i++;
}
return '\0';
}
Implement Strcspn
/*
* This program will check the first unmatched characters of
* target string 'tar' to the source string 'src'
* and returns the total number of first unmatched characters.
* */
int mystrcspn(char *src, char *tar)
{
char *t; /*For retaing the address of tar string*/
int count = 0; /*For counting */
for(;*src != '\0'; src++) {
for(t = tar; *t != '\0'; t++) {
if( *src == *t )
return (count);
}
if(*t == '\0')
count++;
}
return (count);
}
* This program will check the first unmatched characters of
* target string 'tar' to the source string 'src'
* and returns the total number of first unmatched characters.
* */
int mystrcspn(char *src, char *tar)
{
char *t; /*For retaing the address of tar string*/
int count = 0; /*For counting */
for(;*src != '\0'; src++) {
for(t = tar; *t != '\0'; t++) {
if( *src == *t )
return (count);
}
if(*t == '\0')
count++;
}
return (count);
}
Implement strspn
*
* This program will Count the first matched character
* from target string "tar" to the source string "src"
* and returns the value of count.
* */
int mystrspn(const char *src, const char *tar)
{
const char *t = NULL;/*For retaing the address of target string*/
int count = 0; /*For counting*/
for(;*src != '\0'; src++){
for(t = tar; *t != '\0'; t++)
if(*src == *t){
count++;
break;
}
if(*t == '\0')
return(count);
}
return(count);
}
* This program will Count the first matched character
* from target string "tar" to the source string "src"
* and returns the value of count.
* */
int mystrspn(const char *src, const char *tar)
{
const char *t = NULL;/*For retaing the address of target string*/
int count = 0; /*For counting*/
for(;*src != '\0'; src++){
for(t = tar; *t != '\0'; t++)
if(*src == *t){
count++;
break;
}
if(*t == '\0')
return(count);
}
return(count);
}
Implement strrchr
/*
* This program will search for the occurance of a character
* from the last of the string.
* If found it will return the address of last occurance of that character
* */
char *mystrrchr(const char *s1, int x)
{
const char *p = NULL; /*For copying the address of the string for check*/
while(*s1 != '\0'){
if(*s1 == x)
p = s1;
s1++;
}
return(char *)p;
}
* This program will search for the occurance of a character
* from the last of the string.
* If found it will return the address of last occurance of that character
* */
char *mystrrchr(const char *s1, int x)
{
const char *p = NULL; /*For copying the address of the string for check*/
while(*s1 != '\0'){
if(*s1 == x)
p = s1;
s1++;
}
return(char *)p;
}
Implement strncmp
/*This program will compare the n-bit of a string s1 with s2. If both string are same up to n-bits it will return Zero else it will returns the difference of the ascii value of 1st unmatched character.*/
int mystrncmp(const char *s1, const char *s2, int n)
{
while(s1 != '\0' && (*s1++ == *s2++) && --n > 1)
;
return (*s1 - *s2);
}
int mystrncmp(const char *s1, const char *s2, int n)
{
while(s1 != '\0' && (*s1++ == *s2++) && --n > 1)
;
return (*s1 - *s2);
}
Implement strncat
/*This function will append "n" character of source string "src" into target string "tar" & return Zero. */
char *mystrncat(char *tar, const char *src, int n)
{
char *t;/*retaing the address of target string*/
t = tar ;
while (*tar != '\0')
tar++;
/*Copying from src to tar string*/
while (*src != '\0' && (*tar++ = *src++) && (--n > 1));
*tar = '\0';
return t;
}
char *mystrncat(char *tar, const char *src, int n)
{
char *t;/*retaing the address of target string*/
t = tar ;
while (*tar != '\0')
tar++;
/*Copying from src to tar string*/
while (*src != '\0' && (*tar++ = *src++) && (--n > 1));
*tar = '\0';
return t;
}
Implement strncpy
/*This function will copy "n" character of the source string "src" to the target string "tar" and return Zero.*/
char *mystrncpy(char *tar, const char *src, int n)
{
char *t = tar; /*for Retaing the address tar string*/
/*Copying string*/
while(*src != '\0' && (*tar++ = *src++) && n--);
*tar = '\0';
return t;
}
Subscribe to:
Posts (Atom)