VB icon

User defined string Functions (Length and Reversal)

Email
Submitted on: 1/7/2015 3:48:00 PM
By: Karthik A (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: C, Borland C++
Views: 4973
 
     This program explains beginners to write functions to find the length of a string and to reverse a string without using the in-built functions strlen and strrev. Rate the code if you like it and also comment on the code.
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :User defined string Functions (Length and Reversal)
//**************************************
#include<stdio>
#include<conio>
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: User defined string Functions (Length and Reversal)
// Description:This program explains beginners to write functions to find the length of a string and to reverse a string without using the in-built functions strlen and strrev. Rate the code if you like it and also comment on the code.
// By: Karthik A (from psc cd)
//
// Inputs:A word (string)
//
// Returns:Its length and the reversed string.
//
// Assumes:Basics of pointers
//
// Side Effects:NIL
//**************************************

/* This program explains string functions strlen and strrev without the use of in-built functions*/
#include<stdio>
#include<conio>
/* Function to return the length of a string */
int length(char *base)//The argument to this function is the base address of the input string
{
int ct=0;//Counters for accessing the string and count
while(*(base+ct)!='\0')//Increment the counter if '\0' is not encountered
{
ct++;
}
return ct; //Return the length
}
/*Function to reverse the string */
void reverse(char *base,int length) //There are two arguments 1. Base address
{ //2. Length of the string obtained by using length function
int i;
for(i=length-1;i>=0;i--)
{ //i is stored with the length of the string and now can be accessed in the reverse
printf("%c",*(base+i));//The string is printed using pointers with base address and the counter 'i'
}
}
void main()
{
char str[20];
int len;
clrscr();
printf("\n Enter a word:");
scanf("%s",str);
printf("\n Call the function length with the parameter as the string's base address");
len=length(str);//Call the function length with the parameter as the string's base address
printf("\n The length is %d",len);
getch();
/* Strrev */
printf("\n The function reverse is called with two param 1. base address and 2. length");
printf("\n");
reverse(str,len);//The function reverse is called with two param 1. base address and 2. length
getch();
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.