String Manipulation

Submitted by Karthikeyan on

Sample C program to demonstrate various string manipulation functions


#include 
main()
{
    char str[] = "Computer";
    char target[20];
    printf("\n\t String Manipulation");
    printf("\n\t *******************");
    printf("\n\n Strlen");
    printf("\n ******");
    printf("\n The length of the string %s is %d ", str, lenf(str));
    copy(target, str);
    printf("\n\n Strcpy");
    printf("\n ******");
    printf("\n The source strings is %s\n", str);
    printf("\n The target string is %s\n", target);
    printf("\n\n Strcmp");
    printf("\n ******");
    if(compare(str,target)==0)
    printf("\n The strings are %s and %s\n They are equal ", str, target);
    else
    printf("\n The strings are %s and science are equal", str, target);
    if(compare(str, "science") == 0)
    printf("\n The string %s and science are equal\n", str);
    else
    printf("\n The string %s and science are not equal \n", str);
    printf("\n\n Palindrome checking");
    printf("\n *******************");
    if(pal(str, target))
    printf("\n The string %s is a palindrome", str);
    else
    printf("\n The string %s is not a palindrome", str);
    if(pal("window"), target)
    printf("\n The string window is a palindrome");
    else
    printf("\n The string window is not a palindrome");
getch();
}
lenf(char*s)
{
    int l = 0;
    while(*s!='\0')
    {
        l++;
        s++;
    }
    return(l);
}
copy(char*ts, char*ss)
{
    while(*ss != '\0')
    {
        *ts = *ss;
        ss++;
        ts++;
    }
    *ts='\0';
}
compare(char*s1, char*s2)
{
    int i;
    if(lenf(s1)!=lenf(s2))
    {
        i = *s1 - *s2;
        if(i<0)
        return(1);
    }
    else
    return(0);
}
pal(char*s, char*r)
{
    int i,l;
    l = lenf(s);
    for(i=0; i