How String Functions Work? Part II

This is the second part of the article How
String Functions (strinh.h) Work?


Here we'll be designing our own version of some other commonly used standard
library string manipulation function that we discussed in the article String
Manipulation Functions (string.h) II.


These will help increase your programming skills further.


mystrlwr():



// mystrlwr function
#include<iostream.h>

// -- FUNCTION PROTOTYPES --
char *mystrlwr(char *);
// -- ENDS --

void main()
{
char ch[]="C++ ProGramming123";

cout<<mystrlwr(ch);
}

char *mystrlwr(char *str)
{
char *temp;
temp=str;

while(*str!='\0')
{
// change only if its a
// UPPER case character
// intelligent enough not to
// temper with special
// symbols and numbers
if(*str>=65 && *str<=90)
*str+=32;
str++;
}
return temp;;
}


mystrupr():



// mystrupr function
#include<iostream.h>

// -- FUNCTION PROTOTYPES --
char *mystrupr(char *);
// -- ENDS --

void main()
{
char ch[]="C++ ProGramming123";

cout<<mystrupr(ch);
}

char *mystrupr(char *str)
{
char *temp;
temp=str;

while(*str!='\0')
{
// change only if its a
// UPPER case character
// intelligent enough not to
// temper with special
// symbols and numbers
if(*str>=97 && *str<=122)
*str-=32;

str++;
}
return temp;
}

mystrncat():



// mystrncat- function
#include<iostream.h>

void mystrncat(char *,const char *,int);

void main(void)
{
char ch[]="This is great!";
char ch2[25]="Yes ";
mystrncat(ch2,ch,6);

cout<<ch;
cout<<endl;
cout<<ch2;
}

void mystrncat(char *str1,const char *str2,int n)
{
int i=0;
int len=0;

// skip to the end of the first
// string(target)
while(str1[len]!='\0')
len++;

// start copying 'n' characters
// from the start of the
// second string (source)
// to the end of the
// first (target)
while(i<n)
{
str1[len]=str2[i];
i++;len++;
}
str1[len]='\0';
}

mystrncpy():



// mystrncpy- function
#include<iostream.h>

void mystrncpy(char *,const char *,int);

void main(void)
{
char ch[]="This is great!";
char ch2[20];

mystrncpy(ch2,ch,6);

cout<<ch;
cout<<endl;
cout<<ch2;
}

void mystrncpy(char *str1,const char *str2,int n)
{
int i=0;

// copy 'n' characters
// from str2 to str1
while(i<n)
{
str1[i]=str2[i];
i++;
}
// put the end of
// string identifier
str1[i]='\0';
}

mystrrev():



// mysrtrev function

#include<iostream.h>

char *mystrrev(char *);

void main(void)
{
char ch[]="Programming";
cout<<mystrrev(ch);

cout<<ch;
}

char *mystrrev(char *str)
{
char temp;
int len=0;
int i,j;

// find the length
while(str[len]!='\0')
len++;

j=len-1;

for(i=0;i<len/2;i++)
{
// interchange the
// characters from
// the beginning to
// the end
temp=str[j];
str[j]=str[i];
str[i]=temp;
j--;
}

return str;
}

Related Articles:


Check out this stream