Tuesday, October 13, 2009

string manipulation without library

#include
#include
#include
/*b. Without using library
*
function doing the above operation.
1.string copy,
2.string length
3. String concatenation and
study the function like stricmp, strrev.*/
int main()
{
char str[100];
char str1[100];
char temp[100];
printf("enter the string\n");
scanf("%s",str);
int k,i=0,j=0; //1.string length
while(str[i]!='\0')
{
i++;
}printf("\nstring %s length is:%d\n",str,i);

int length=i; //2.string copy
for(k=0;k<length;k++)
{
str1[k]=str[k];
}
str1[k]='\0';
printf("\n string copy str to str1 like %s to %s\n",str,str1);
//string concatenation
i=0;
while(str1[i]!='\0')
{
i++;
}printf("\n string %s length is:%d\n",str1,i);
int length2=i;
for(k=0;k<(length2+length);k++)
{
if(k {
str[k]=str[k];
}else
{
str[k]=str1[j];
j++;
}
}
str[k] = '\0';
printf("string concatenation like :%s\n",str);

//string reverse
j=0;
for(k=length2-1;k>=0;k--)
{
temp[j]=str1[k];
j++;
}
temp[j]='\0';
printf("\n string reverse %s is %s",str1,temp);
//6.stricmp
printf("\n enter the first string\n");
scanf("%s",str);
printf("enter the second string\n");
scanf("%s",str1);
i=0;
while(str[i]!='\0')
{
i++;
}length=i;
while(str1[i]!='\0')
{
i++;
}length2=i;
if(length==length2)
{
k=0;i=0;
while(k<length)
{
if(str[k]==str1[k] || str[k]==str1[k]+32 || str[k]==str1[k]-32)
{
i++;
}
k++;
}if(i==length)
{
printf("str %s and str1 %s is matched",str,str1);
}else
{
printf("str %s and str1 %s is not matched",str,str1);
}
}
getch();
return 0;
}

No comments:

Post a Comment