Tuesday, May 19, 2009

String function i.e. Search, Equal ,Smaller, Greater,Getsub,LeftSub,Rightsub , Upper, Lower ,Reverse,Replace

#include
#include
#include
#include
String function i.e. Search, Equal ,Smaller, Greater,Getsub,LeftSub,Rightsub , Upper, Lower ,Reverse,Replace type function

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 ;
}

Program to search for a string into another string.

/* Program to search for a string into another string. */

#include
#include
#include

int xstrsearch ( char *, char * ) ;
void show( ) ;

void main( )
{
char s1[ ] = "NagpurKicit" ;
char s2[ ] = "Kicit" ;
int pos ;

clrscr( ) ;

printf ( "String s1: %s\n", s1 ) ;

printf ( "String s2: %s\n", s2 ) ;

/* search if s2 is present in s1 */
pos = xstrsearch ( s1, s2 ) ;
printf ( "\nThe pattern string is found at position: %d\n", pos ) ;

getch( ) ;
}

/* searches for the given pattern s2 into the string s1 */
int xstrsearch ( char * s1, char * s2 )
{
int i, j, k ;
int l1 = strlen ( s1 ) ;
int l2 = strlen ( s2 ) ;

for ( i = 0 ; i <= l1 - l2 ; i++ )
{
j = 0 ;
k = i ;
while ( ( s1[k] == s2[j] ) && ( j < l2 ) )
{
k++ ;
j++ ;
}
if ( j == l2 )
return i ;
}
return -1 ;
}

Program to allocate memory dynamically for strings, and store their addresses in array of pointers to strings

/* Program to allocate memory dynamically for strings, and store
their addresses in array of pointers to strings. */
#include
#include
#include
#include

void main( )
{
char *name[5] ;
char str[20] ;
int i ;

clrscr( ) ;

for ( i = 0 ; i < 5 ; i++ )
{
printf ( "Enter a String: " ) ;
gets ( str ) ;
name[i] = ( char * ) malloc ( strlen ( str ) + 1 ) ;
strcpy ( name[i], str ) ;
}

printf ( "\nThe strings are:" ) ;

for ( i = 0 ; i < 5 ; i++ )
printf ( "\n%s", name[i] ) ;

for ( i = 0 ; i < 5 ; i++ )
free ( name[i] ) ;

getch( ) ;
}

Program to check entered name in the master list.

/* Program to check entered name in the master list. */

#include
#include
#include

#define MAX1 6
#define MAX2 10

char masterlist[MAX1][MAX2] ;
int count ;

int add ( char *s ) ;
int find ( char *s ) ;

void main( )
{
char yourname[MAX2] ;
int flag ;

clrscr( ) ;
flag = add ( "akshay" ) ;
if ( flag == 0 )
printf ( "\nUnable to add string" ) ;

flag = add ( "parag" ) ;
if ( flag == 0 )
printf ( "\nUnable to add string" ) ;

flag = add ( "raman" ) ;
if ( flag == 0 )
printf ( "\nUnable to add string" ) ;

flag = add ( "srinivas" ) ;
if ( flag == 0 )
printf ( "\nUnable to add string" ) ;

flag = add ( "gopal" ) ;
if ( flag == 0 )
printf ( "\nUnable to add string" ) ;

flag = add ( "rajesh" ) ;
if ( flag == 0 )
printf ( "Unable to add string" ) ;

printf ( "Enter your name: " ) ;
gets ( yourname ) ;
flag = find ( yourname ) ;
if ( flag == 1 )
printf ( "Welcome, you can enter the palace\n" ) ;
else
printf ( "Sorry, you are a trespasser" ) ;

getch( ) ;
}

/* adds string to the array */
int add ( char *s )
{
if ( count < MAX1 )
{
if ( strlen ( s ) < MAX2 )
{
strcpy ( &masterlist[count][0], s ) ;
count++ ;
return 1 ;
}
}

return 0 ;
}

/* finds the given string */
int find ( char *s )
{
int flag = 0, i ;

for ( i = 0 ; i < count ; i++ )
{
if ( strcmp ( &masterlist[i][0], s ) == 0 )
{
flag = 1 ;
break ;
}
}

return flag ;
}

Program to perform some basic operations on string.

/* Program to perform some basic operations on string. */

#include
#include
#include

int xstrlen ( char * ) ;
void xstrcpy ( char *, char * ) ;
void xstrcat ( char *, char * ) ;
int xstrcmp ( char *, char * ) ;
void show ( char * ) ;

void main( )
{
char s1[ ] = "kicit" ;
char s2[ ] = "Nagpur" ;
char s3[20] ;
int len ;

clrscr( ) ;

printf ( "\nString s1: %s", s1 ) ;
len = xstrlen ( s1 ) ;
printf ( "\nlength of the string s1: %d", len ) ;

printf ( "\nString s2: %s", s2 ) ;

xstrcpy ( s3, s1 ) ;
printf ( "\nString s3 after copying s1 to it: %s", s3 ) ;

xstrcat ( s3, s2 ) ;
printf ( "\nString s3 after concatenation: %s", s3 ) ;

if ( xstrcmp ( s1, s2 ) == 0 )
printf ( "\nThe strings s1 and s2 are similar" ) ;
else
printf ( "\nThe strings s1 and s2 are not similar" ) ;

getch( ) ;
}

/* finds the length of the string */
int xstrlen ( char *s )
{
int l = 0 ;
while ( *s )
{
l++ ;
s++ ;
}
return l ;
}

/* copies source string s to the target string t */
void xstrcpy ( char *t, char *s )
{
while ( *s )
{
*t = *s ;
t++ ;
s++ ;
}
*t = '\0' ;
}

/* concatenates the two strings */
void xstrcat ( char *t, char *s )
{
while ( *t )
t++ ;
while ( *s )
*t++ = *s++ ;
*t = '\0' ;
}

/* compares two strings s and t for equality */
int xstrcmp ( char *s, char *t )
{
while ( *s == *t )
{
if ( ! ( *s ) )
return 0 ;
s++ ;
t++ ;
}
return ( *s - *t ) ;
}

Featured post

Sitecore 10 Installation on Docker