Important alert: (current site time 7/15/2013 5:22:11 AM EDT)
 

VB icon

String Functions

Email
Submitted on: 4/15/2013 11:20:36 AM
By: Jake Rodriguez Pomperada  
Level: Beginner
User Rating: By 2 Users
Compatibility: C
Views: 819
author picture
(About the author)
 
     If you like my work please send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com. Here in the Philippines people can reach me through my mobile numbers 09993969756, 09154628025 and 09288471599. My telephone number at home is +63 (034) 4335081. I am also working as a freelance developer if you like to acquire my programming services kindly contact me in my email address above and mobile phone numbers. Thank you very much and Happy Programming. Regards, Mr. Jake Rodriguez Pomperada, MAED-IT Teacher, Software Developer, Web Developer and Electronics and Computer Technician Share My Code Freely Without Any Cost. Product of the Philippines.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: String Functions
// Description:If you like my work please send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com. Here in the Philippines people can reach me through my mobile numbers 09993969756, 09154628025 and 09288471599. My telephone number at home is +63 (034) 4335081. I am also working as a freelance developer if you like to acquire my programming services kindly contact me in my email address above and mobile phone numbers. Thank you very much and Happy Programming. Regards, Mr. Jake Rodriguez Pomperada, MAED-IT Teacher, Software Developer, Web Developer and Electronics and Computer Technician Share My Code Freely Without Any Cost. Product of the Philippines.
// By: Jake Rodriguez Pomperada
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=14100&lngWId=3//for details.//**************************************

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <alloc.h>
int search ( char *, char ) ;
int isequals ( char *, char * ) ;
int issmaller ( char *, char * ) ;
int isgreater ( char *, char * ) ;
char * getsub ( char *, int, int ) ;
char * leftsub ( char *, int n ) ;
char * rightsub ( char *, int n ) ;
void upper ( char * ) ;
void lower ( char * ) ;
void reverse ( char * ) ;
int replace ( char *, char, char ) ;
int setat ( char *, char, int ) ;
void main( )
{
	char s1[ ] = "Hello" ;
	char s2[ ] = "Hello World" ;
	char s3[ ] = "Four hundred thirty two" ;
	char ch, *s ;
	int i ;
	clrscr( ) ;
	printf ( "\nString s1: %s", s1 ) ;
	/* check for the first occurrence of a character */
	printf ( "\nEnter character to search: " ) ;
	scanf ( "%c", &ch ) ;
	i = search ( s1, ch ) ;
	if ( i != -1 )
		printf ( "The first occurrence of character %c is found at index no.
%d\n", ch, i ) ;
	else
		printf ( "Character %c is not present in the list.\n", ch ) ;
	printf ( "\nString s2: %s", s2 ) ;
	/* compares two strings s1 and s2 */
	i = isequals ( s1, s2 ) ;
	if ( i == 1 )
		printf ( "\nStrings s1 and s2 are identical" ) ;
	else
		printf ( "\nStrings s1 and s2 are not identical") ;
	i = issmaller ( s1, s2 ) ;
	if ( i == 1 )
		printf ( "\nString s1 is smaller than string s2" ) ;
	else
		printf ( "\nString s1 is not smaller than string s2" ) ;
	i = isgreater ( s1, s2 ) ;
	if ( i == 1 )
		printf ( "\nString s1 is greater than string s2\n" ) ;
	else
		printf ( "\nString s1 is not greater than string s2\n" ) ;
	/* extract characters at given position */
	printf ( "\nString s3: %s", s3 ) ;
	s = getsub ( s3, 5, 7 ) ;
	printf ( "\nSub string: %s", s ) ;
	free ( s ) ;
	/* extract leftmost n characters */
	s = leftsub ( s3, 4 ) ;
	printf ( "\nLeft sub string: %s", s ) ;
	free ( s ) ;
	/* extract rightmost n characters */
	s = rightsub ( s3, 3 ) ;
	printf ( "\nRight sub string: %s", s ) ;
	free ( s ) ;
	/* convert string to uppercase */
	upper ( s3 ) ;
	printf ( "\nString in upper case: %s", s3 ) ;
	/* convert string to lowercase */
	lower ( s3 ) ;
	printf ( "\nString in lower case: %s", s3 ) ;
	/* reverse the given string */
	reverse ( s3 ) ;
	printf ( "\nReversed string: %s", s3 ) ;
	/* replace first occurrence of one char with new one */
	replace ( s1, 'H' , 'M' ) ;
	printf ( "\nString s1: %s", s1 ) ;
	/* sets a char at a given position */
	i = setat ( s1, 'M', 3 ) ;
	if ( i )
		printf ( "\nString s1: %s", s1 ) ;
	else
		printf ( "\nInvalid position." ) ;
	getch( ) ;
}
/* check for the first occurrence of a character */
int search ( char *str, char ch )
{
	int i = 0 ;
	while ( *str )
	{
		if ( *str == ch )
			return i ;
		str++ ;
		i++ ;
	}
	return -1 ;
}
/* checks whether two strings are equal */
int isequals ( char *s, char *t )
{
	while ( *s || *t )
	{
		if ( *s != *t )
			return 0 ;
		s++ ;
		t++ ;
	}
	return 1 ;
}
/* checks whether first string is less than second */
int issmaller ( char *s, char *t )
{
	while ( *t )
	{
		if ( *s != *t )
		{
			if ( *s < *t )
				return 1 ;
			else
				return 0 ;
		}
		t++ ;
		s++ ;
	}
	return 0 ;
}
/* checks whether first string is greater than second */
int isgreater ( char *s, char *t )
{
	while ( *s )
	{
		if ( *s != *t )
		{
			if ( *s > *t )
				return 1 ;
			else
				return 0 ;
		}
		s++ ;
		t++ ;
	}
	return 0 ;
}
/* extracts the character at given position */
char * getsub ( char *str, int spos, int n )
{
	char *s = str + spos ;
	char *t = ( char * ) malloc ( n + 1 ) ;
	int i = 0 ;
	while ( i < n )
	{
		t[i] = *s ;
		s++ ;
		i++ ;
	}
	t[i] = '\0' ;
	return t ;
}
/* extracts leftmost n characters from the string */
char * leftsub ( char *s, int n )
{
	char *t = ( char * ) malloc ( n + 1 ) ;
	int i = 0 ;
	while ( i < n )
	{
		t[i] = *s ;
		s++ ;
		i++ ;
	}
	t[i] = '\0' ;
	return t ;
}
/* extracts rightmost n characters from the string */
char * rightsub ( char *str, int n )
{
	char *t = ( char * ) malloc ( n + 1 ) ;
	int l = strlen ( str ) ;
	char *s = str + ( l - n ) ;
	int i = 0 ;
	while ( i < n )
	{
		t[i] = *s ;
		s++ ;
		i++ ;
	}
	t[i] = '\0' ;
	return t ;
}
/* converts string to uppercase */
void upper ( char *s )
{
	while ( *s )
	{
		if ( *s >= 97 && *s <= 123 )
			*s -= 32 ;
		s++ ;
	}
}
/* converts string to lowercase */
void lower ( char *s )
{
	while ( *s )
	{
		if ( *s >= 65 && *s <= 91 )
			*s += 32 ;
		s++ ;
	}
}
/* reverses a string */
void reverse ( char *str )
{
	int l = strlen ( str ) ;
	char ch, *t = ( str + l - 1 ) ;
	int i = 0 ;
	while ( i < l / 2 )
	{
		ch = *str ;
		*str = *t ;
		*t = ch ;
		str++ ;
		t-- ;
		i++ ;
	}
}
/* replaces the first occurrence of char with new char */
int replace ( char *str, char oldch, char newch )
{
	while ( *str )
	{
		if ( *str == oldch )
		{
			*str = newch ;
			return 1 ;
		}
		str++ ;
	}
	return 0 ;
}
/* sets a char at a given position */
int setat ( char *str, char ch, int i )
{
	if ( i < 0 || strlen ( str ) < i )
		return 0 ;
	* ( str + i ) = ch ;
	return 1 ;
}


Other 225 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.