0% found this document useful (0 votes)
69 views7 pages

Ds Lab Assignment 6

The document contains code snippets for: 1) Summing the elements of an integer array using pointers. 2) Reversing a string by: a) Iterating from the beginning and end inward. b) Using a pointer based approach. 3) Checking if a string is a palindrome by: a) Comparing first and last characters and iterating inward. b) Using pointers to compare characters from beginning and end.

Uploaded by

S.ganga priya
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)
69 views7 pages

Ds Lab Assignment 6

The document contains code snippets for: 1) Summing the elements of an integer array using pointers. 2) Reversing a string by: a) Iterating from the beginning and end inward. b) Using a pointer based approach. 3) Checking if a string is a palindrome by: a) Comparing first and last characters and iterating inward. b) Using pointers to compare characters from beginning and end.

Uploaded by

S.ganga priya
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/ 7

1)sum of array of elements using pointers

#include<stdio.h>

int sum_array(const int [],int);

int main()

int a[100],i;

int n;

printf("enter how many numbers you want:");

scanf("%d",&n);

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

scanf("%d",&a[i]);

int result;

result=sum_array(a,n);

printf("%d",result);

int sum_array(const int a[],int n)

int *p,sum=0;

p=&a[0];

for(p=&a[0];p<&a[n];p++)

sum+=*p;

return sum;

OUTPUT: enter how many numbers you want:5

5
15

2)
a)reverse of string
#include<stdio.h>

int main(){

char s[100];

int i;

printf("enter the message:\n");

for(i=0;;i++)

s[i]=getchar();

if(s[i]=='\n')

break;

printf("the reverse statement is:\n");

for(;i>=0;i--)

putchar(s[i]);

putchar('\n');

return 0;

OUTPUT: enter the message:

hello world

the reverse statement is:

dlrow olleh

b) reverse of string using pointers


#include<stdio.h>

int string_length(char*);

void reverse(char*);
main()

char string[100];

printf("Enter a string\n");

gets(string);

reverse(string);

printf("Reverse of entered string is \"%s\".\n", string);

return 0;

void reverse(char *string)

int length, c;

char *begin, *end, temp;

length = string_length(string);

begin = string;

end = string;

for ( c = 0 ; c < ( length - 1 ) ; c++ )

end++;

for ( c = 0 ; c < length/2 ; c++ )

temp = *end;
*end = *begin;

*begin = temp;

begin++;

end--;

int string_length(char *pointer)

int c = 0;

while( *(pointer+c) != '\0' )

c++;

return c;

OUTPUT: Enter a string

HELLO WORLD

Reverse of entered string is "DLROW OLLEH".

3)
a) string is palindrome or not
#include<stdio.h>

#include <string.h>

int checkpalindrome(char *s)

int i,c=0,n;

n=strlen(s);

for(i=0;i<n/2;i++)
{

if(s[i]==s[n-i-1])

c++;

if(c==i)

return 1;

else

return 0;

int main()

char s[1000];

printf("Enter the string: ");

gets(s);

if(checkpalindrome(s))

printf("string is palindrome");

else

printf("string is not palindrome");

OUTPUT: Enter the string: he lived a devil eh


string is palindrome

b)string is palindrome using pointers


#include <stdio.h>

#include <string.h>

int solve(char* string);

int solve(char *string)

int length;

char *forward, *reverse;

length = strlen(string);

forward = string;

reverse = forward + length - 1;

for (forward = string; reverse >= forward;)

if (*reverse == *forward)

reverse--;

forward++;

else

break;

if (forward > reverse)

return 1;

else

return 0;

int main()

int result;

char string[100];
printf("enter string:");

gets(string);

result= solve(string);

if(result==1)

printf("palindrome");

else

printf("not palindrome");

OUTPUT: enter string:Madam I am Adam

not palindrome

You might also like