0% found this document useful (0 votes)
26 views5 pages

Stringsass

The document contains 6 code snippets that demonstrate C programs to perform various string operations: 1) Find the length of a string, 2) Compare two strings, 3) Copy one string to another, 4) Concatenate two strings, 5) Reverse a string, and 6) Check if a string is a palindrome. Each snippet includes the necessary header files, defines a function to perform the operation, includes a main function to call and test the function.

Uploaded by

Sagar Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Stringsass

The document contains 6 code snippets that demonstrate C programs to perform various string operations: 1) Find the length of a string, 2) Compare two strings, 3) Copy one string to another, 4) Concatenate two strings, 5) Reverse a string, and 6) Check if a string is a palindrome. Each snippet includes the necessary header files, defines a function to perform the operation, includes a main function to call and test the function.

Uploaded by

Sagar Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1. WAP to find the length of a string.

//WAP to find the length of a string.


#include <stdio.h>
int mystrlen(char a[])
{
int i, c = 0;

for (i = 0; a[i] != '\0'; i++)


{
c++;
}
return c;
}

int main()
{
int l;
char a[20];

printf("Enter a string: ");


gets(a);

l = mystrlen(a);

printf("The length of %s is %d.", a, l);

return 0;
}

2. WAP to find the length of a string.


//WAP to compare two strings.
#include <stdio.h>
int mystrcmp(char a[], char b[])
{
int i;

for (i = 0; a[i] != '\0' && b[i] != '\0' && a[i] == b[i]; i++)
;
{
return a[i] - b[i];
return 1;
}
}

int main()
{
int cmp;
char a[20], b[20];

printf("Enter a string: ");


gets(a);
printf("Enter another string: ");
gets(b);

cmp = mystrcmp(a, b);


printf("The value of comparision is %d\n", cmp);

if (cmp < 0)
{
printf("First string is lesser than second.");
}

else if (cmp > 0)


{
printf("First string is greater than second.");
}

else
{
printf("Both strings are same.");
}
return 0;
}

3. WAP to find the length of a string.

//WAP to copy two strings.


#include <stdio.h>
void mystrcpy(char a[], char b[])
{
int i;
for (i = 0; b[i] != '\0'; i++)
{
a[i] = b[i];
}
a[i] = '\0';
puts(a);
}

int main()
{
char a[20], b[20];
printf("Enter a string: ");
gets(b);
mystrcpy(a, b);
printf("Now a=%s", a);
return 0;
}
4. WAP to find the length of a string.

//WAP to concatenate two strings.


#include <stdio.h>
#include <string.h>
void mystrcont(char a[], char b[])
{
int l1, l2, i, j, k;
char ch[30];
l1 = strlen(a);
l2 = strlen(b);
for (i = 0; i < l1; i++)
{
ch[i] = a[i];
}
k = i;

for (i = 0; i < l2; i++)


{
ch[k] = b[i];
k++;
}
for (i = 0; i < l1 + l2; i++)
{
printf("%c", ch[i]);
}
}

int main()
{
char a[20], b[20];
printf("Enter a string: ");
gets(a);
printf("Enter another string: ");
gets(b);

mystrcont(a, b);

return 0;
}

5. WAP to find the length of a string.

//WAP to reverse a string.


#include <stdio.h>
#include <string.h>
void mystrrev(char a[])
{
int i, l, t;
l = strlen(a);
for (i = 0; i < l / 2; i++)
{
t = a[i];
a[i] = a[l - i - 1];
a[l - i - 1] = t;
}
}

int main()
{
char a[20];

printf("Enter a string: ");


gets(a);
mystrrev(a);
printf("After reversed it is:%s", a);

return 0;
}

6. WAP to find the length of a string.

//WAP to check whether a string is palindrome or not.


#include <stdio.h>
#include <string.h>
void mystrpal(char a[])
{
int i, l, t, v;
char b[20];
strcpy(b, a);
l = strlen(a);
for (i = 0; i < l / 2; i++)
{
t = a[i];
a[i] = a[l - i - 1];
a[l - i - 1] = t;
}
v = strcmp(a, b);
if (v == 0)
printf("This string is a palindrome.");
else
printf("This string is not a palindrome.");
}

int main()
{
char a[20];

printf("Enter a string: ");


gets(a);
mystrpal(a);
return 0;
}

You might also like