473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

need help in pointers in C

hi friends,
i am new to this can any one help me regarding this issue
my aim is to Write a program which computes the largest palindrome substring of a string using pointers

Input: The input is the string whose largest palindrome substring should be found.

Output: The output displays the largest palindrome substring.

Sample Input:

Enter the string: Habitat

Sample Output:

The longest palindrome substring of Habitat is: tat
----------------------------------------------------------------------------------------------------------------
i am able to write the code with out using pointers can any one help me regarding this how can i write this using pointers

code with out pointers is
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. main()
  5. {
  6.  char s[20],x[20],y[10],t[10];
  7.  int i,j,l,k,l1=0;
  8.  printf("Enter the string");
  9.  gets(s);
  10.  l=strlen(s);
  11.  s[l]='\0';
  12.  for(i=0;i<l;i++)
  13.  {
  14.    j=0;
  15.    x[j]=s[i];         //taking the first character into X
  16.    j++;
  17.    x[j]='\0';
  18.    for(k=i+1;k<l;k++)
  19.     {
  20.       x[j]=s[k];     
  21.       j++;
  22.       x[j]='\0';
  23.       strcpy(t,x);
  24.     if(strcmp(t,strrev(x))==0) //comparing the substring as if palindrome  
  25.        {
  26.      if(l1<strlen(x))  //if palindrome compare the length wih previous paindrome
  27.      {
  28.        strcpy(y,t);
  29.        puts(y);          //print the longer palindrome
  30.        l1=strlen(y);
  31.      }
  32.     }
  33.  
  34.     strrev(x);
  35.       }
  36.  
  37.    }
  38.  getch();
  39.  }
Aug 17 '07 #1
3 1544
Meetee
931 Expert Mod 512MB
hi friends,
i am new to this can any one help me regarding this issue
my aim is to Write a program which computes the largest palindrome substring of a string using pointers

Input: The input is the string whose largest palindrome substring should be found.

Output: The output displays the largest palindrome substring.

Sample Input:

Enter the string: Habitat

Sample Output:

The longest palindrome substring of Habitat is: tat
----------------------------------------------------------------------------------------------------------------
i am able to write the code with out using pointers can any one help me regarding this how can i write this using pointers

code with out pointers is

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char s[20],x[20],y[10],t[10];
int i,j,l,k,l1=0;
printf("Enter the string");
gets(s);
l=strlen(s);
s[l]='\0';
for(i=0;i<l;i++)
{
j=0;
x[j]=s[i]; //taking the first character into X
j++;
x[j]='\0';
for(k=i+1;k<l;k++)
{
x[j]=s[k];
j++;
x[j]='\0';
strcpy(t,x);
if(strcmp(t,strrev(x))==0) //comparing the substring as if palindrome
{
if(l1<strlen(x)) //if palindrome compare the length wih previous paindrome
{
strcpy(y,t);
puts(y); //print the longer palindrome
l1=strlen(y);
}
}

strrev(x);
}

}
getch();
}
Hi,

You can use array of char like char * x = new char[]; instead of char x[n]. I am not able to find anything else in this code where you can use pointers.

Regards
Aug 17 '07 #2
ilikepython
844 Expert 512MB
hi friends,
i am new to this can any one help me regarding this issue
my aim is to Write a program which computes the largest palindrome substring of a string using pointers

Input: The input is the string whose largest palindrome substring should be found.

Output: The output displays the largest palindrome substring.

Sample Input:

Enter the string: Habitat

Sample Output:

The longest palindrome substring of Habitat is: tat
----------------------------------------------------------------------------------------------------------------
i am able to write the code with out using pointers can any one help me regarding this how can i write this using pointers

code with out pointers is

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char s[20],x[20],y[10],t[10];
int i,j,l,k,l1=0;
printf("Enter the string");
gets(s);
l=strlen(s);
s[l]='\0';
for(i=0;i<l;i++)
{
j=0;
x[j]=s[i]; //taking the first character into X
j++;
x[j]='\0';
for(k=i+1;k<l;k++)
{
x[j]=s[k];
j++;
x[j]='\0';
strcpy(t,x);
if(strcmp(t,strrev(x))==0) //comparing the substring as if palindrome
{
if(l1<strlen(x)) //if palindrome compare the length wih previous paindrome
{
strcpy(y,t);
puts(y); //print the longer palindrome
l1=strlen(y);
}
}

strrev(x);
}

}
getch();
}
Maybe you could use pointers to iterate over the string:
Expand|Select|Wrap|Line Numbers
  1. char test[] = "Hello World";
  2. char *p;
  3. for (p = test; *p != '\0'; p++;)
  4. {
  5.     // here use "*p" to get the char instead of "test[i]"
  6. }
  7.  
Aug 17 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
It looks like you are going to a lot of unnecessary work. A palindrome is a word or phrase that reads the saem in both directions.

So, you create two pointers A and B. Start A with the the address of element 0 and start B with the address of the element returned by strlen().

Compare the characters pointed at by the two pointers. If they are equal advance A by 1 and reduce B by 1 and repeat the compare.

You have a palindrome when the addresses in A and B are the same.
Aug 18 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
9
by: Simon | last post by:
Hi, if I have a structure like... struct sMyPointers{ ClassA *m_pPointerA; ClassB *m_pPointerB; void *m_pPointerToSomethingElse; };
0
by: rokuingh | last post by:
ok, so i've been working on this one for quite a while, and the code is very big so i'm just going to give the relevant parts. this is a program that builds polymers (chemical structures of repeated...
1
by: ArcInversion | last post by:
Hi, I've been using a javascript script to create a dragon that flies across the page. Anyways, I'd like to make it so when you click the dragon it takes you to a new page. Was wondering if anyone...
23
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
11
by: sarathy | last post by:
Hi, I have been using C++ for a while. I am not entirely clear with the concepts of reference in C++. - Why was there a need for introducing a concept called "Reference" in C++ when everything...
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
7
by: DDP3000 | last post by:
void F(int **A, int N) { int i,j; for(i=0;i<N;i++) for(j=0;j<N;j++) A=((i+j)%2==0)?1:-1; } I have never used such a thing before, so it might be a really stupid question but I cannot find...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.