Since the last few posts of our C programming language tutorial, we are continuing on array – character type. Actually string in C Language programming is a vast chapter and needs more time. We’ll see more and more programs on string in this C language programming tutorial.
Another string function is void strcpy (char ch1[],char ch2[]).This function copies one string to another. The first argument is the destination and the second argument is the source. The value in the second array will copied on the first array. If the first array has any value then the value will be lost and it will hold the value of the second argument, while the value in the second array will remain unchanged.
#include< stdio.h >
#include< string.h >
void main()
{
char ch1[50],ch2[50];
int i;
clrscr();
puts("Enter the first string:-");
gets(ch1);
puts ("Enter the second string:-");
gets (ch2);
printf ("Before calling the function the strings are:-\n");
printf ("%s", ch1);
printf ("\n%s", ch2);
strcpy (ch1, ch2);
printf ("\nAfter calling the function, the strings are:-\n");
printf ("%s", ch1);
printf ("\n%s", ch2);
getch ();
}
}
Palindrome checking program on string
.
#include< stdio.h >
#include< string.h >
void main ()
{
char ch1 [50], ch2 [50];
int i, x=0;
clrscr ();
puts ("Enter the string:-");
gets (ch1);
for (i=strlen (ch1)-1; i >=0; i--)
{
ch2[x]=ch1 [i];
x++;
}
ch2[x] ='\0';
if (strcmp (ch1, ch2) ==0)
puts ("The string is palindrome.");
else
puts ("The string is not palindrome.");
getch ();
}
palindrome program without using string reverse
ReplyDeleteThx so much
ReplyDelete