string question

how do i make the compiler recognize if a string is a palindrome or not?thanks to anyone that helps.
«1

Comments

  • : how do i make the compiler recognize if a string is a palindrome or not?thanks to anyone that helps.
    :
    probably have your program go through to the end of the word, and check each letter from the end in. IE:take the word THE and have it go to "E" and read in to "T"
    and then have your program read each letter forwards. if the two are the same, it's a palindrome. I would also suggest making Palindrome class or something, so that you could just call it, instead of writing the code each time. Sorry I can't help you mor...I am not that great with strings and streams....although I think I might be able to produce something if given a day or two.
    [italic]I have studied long and hard. All I learned was that out of all the laws in the universe, one is outstanding and absolute. Nothing is for certain.[/italic]

  • : : how do i make the compiler recognize if a string is a palindrome or not?thanks to anyone that helps.
    : :
    : probably have your program go through to the end of the word, and check each letter from the end in. IE:take the word THE and have it go to "E" and read in to "T"
    : and then have your program read each letter forwards. if the two are the same, it's a palindrome. I would also suggest making Palindrome class or something, so that you could just call it, instead of writing the code each time. Sorry I can't help you mor...I am not that great with strings and streams....although I think I might be able to produce something if given a day or two.
    : [italic]I have studied long and hard. All I learned was that out of all the laws in the universe, one is outstanding and absolute. Nothing is for certain.[/italic]
    :
    i understand what your saying buy i have no idea how the hell i would do that. thanks for your help
    :

  • well...I have something about it in my book. The program parses trough the word letter by letter and reads them off. I think I could edit it so that it goes backwards instead, but it is a LOT of code. If no one can think up a relatively short way in 12 12-24 hours email me. I'll grab my book, and start at it. Sorry I can't help you more.
    [italic]I have studied long and hard. All I learned was that out of all the laws in the universe, one is outstanding and absolute. Nothing is for certain.[/italic]

  • : well...I have something about it in my book. The program parses trough the word letter by letter and reads them off. I think I could edit it so that it goes backwards instead, but it is a LOT of code. If no one can think up a relatively short way in 12 12-24 hours email me. I'll grab my book, and start at it. Sorry I can't help you more.
    : [italic]I have studied long and hard. All I learned was that out of all the laws in the universe, one is outstanding and absolute. Nothing is for certain.[/italic]
    :
    thanks alot for your help i really appreciate it , problem is i need this by tommorow morning at 8:30 am. maybe some one else knows anything about this? thanks again.
    :

  • : : well...I have something about it in my book. The program parses trough the word letter by letter and reads them off. I think I could edit it so that it goes backwards instead, but it is a LOT of code. If no one can think up a relatively short way in 12 12-24 hours email me. I'll grab my book, and start at it. Sorry I can't help you more.
    : : [italic]I have studied long and hard. All I learned was that out of all the laws in the universe, one is outstanding and absolute. Nothing is for certain.[/italic]
    : :
    : thanks alot for your help i really appreciate it , problem is i need this by tommorow morning at 8:30 am. maybe some one else knows anything about this? thanks again.
    : :
    :
    :
    I'll grab my book before I go to bed. Check the board early in the morning, sorry if I can't figure it out. either way, I'll notify you.
    [italic]I have studied long and hard. All I learned was that out of all the laws in the universe, one is outstanding and absolute. Nothing is for certain.[/italic]

  • : : : well...I have something about it in my book. The program parses trough the word letter by letter and reads them off. I think I could edit it so that it goes backwards instead, but it is a LOT of code. If no one can think up a relatively short way in 12 12-24 hours email me. I'll grab my book, and start at it. Sorry I can't help you more.
    : : : [italic]I have studied long and hard. All I learned was that out of all the laws in the universe, one is outstanding and absolute. Nothing is for certain.[/italic]
    : : :
    : : thanks alot for your help i really appreciate it , problem is i need this by tommorow morning at 8:30 am. maybe some one else knows anything about this? thanks again.
    : : :
    thank you very very much
    : :
    : :
    : I'll grab my book before I go to bed. Check the board early in the morning, sorry if I can't figure it out. either way, I'll notify you.
    : [italic]I have studied long and hard. All I learned was that out of all the laws in the universe, one is outstanding and absolute. Nothing is for certain.[/italic]
    :
    :

  • : how do i make the compiler recognize if a string is a palindrome or not?thanks to anyone that helps.
    :

    [code]

    int palindrome(char *str)
    {
    int i, j, retval;

    i = 0;
    j = strlen(str);
    retval = 1;
    while (i <= j)
    {
    --j;
    if (str[i] != str[j])
    {
    retval = 0;
    break;
    }
    i++;
    }
    return retval; // return 1 if str is a palindrome, else return 0
    }

    [/code]

    If the string length is odd, there will be one unnecessary comparison, but who cares?

    Note that this routine is case sensitive. If case is not an issue in your application, change the comparison to if(toupper(str[i]) != toupper(str[j])).



  • : : how do i make the compiler recognize if a string is a palindrome or not?thanks to anyone that helps.
    : :
    :
    : [code]
    :
    : int palindrome(char *str)
    : {
    : int i, j, retval;
    :
    : i = 0;
    : j = strlen(str);
    : retval = 1;
    : while (i <= j)
    : {
    : --j;
    : if (str[i] != str[j])
    : {
    : retval = 0;
    : break;
    : }
    : i++;
    : }
    : return retval; // return 1 if str is a palindrome, else return 0
    : }
    :
    : [/code]
    :
    : If the string length is odd, there will be one unnecessary comparison, but who cares?
    :
    : Note that this routine is case sensitive. If case is not an issue in your application, change the comparison to if(toupper(str[i]) != toupper(str[j])).
    :
    :
    :
    :
    Thanks Jeff, I have no idea what that code says...but I'm sure it works.
    [italic]I have studied long and hard. All I learned was that out of all the laws in the universe, one is outstanding and absolute. Nothing is for certain.[/italic]

  • : : how do i make the compiler recognize if a string is a palindrome or not?thanks to anyone that helps.
    : :
    :
    : [code]
    :
    : int palindrome(char *str)
    : {
    : int i, j, retval;
    :
    : i = 0;
    : j = strlen(str);
    : retval = 1;
    : while (i <= j)
    : {
    : --j;
    : if (str[i] != str[j])
    : {
    : retval = 0;
    : break;
    : }
    : i++;
    : }
    : return retval; // return 1 if str is a palindrome, else return 0
    : }
    :
    : [/code]
    :
    : If the string length is odd, there will be one unnecessary comparison, but who cares?
    :
    : Note that this routine is case sensitive. If case is not an issue in your application, change the comparison to if(toupper(str[i]) != toupper(str[j])).
    :
    :ok i tried using your code and i can only get it to return a 0 and never a 1, i used a little print test statement at hte end too to see what it was returning. can you see a a problem?

    int palindrome(char str[]);
    int main()
    {

    char str[20];

    int temp;
    int retval;
    printf("enter a string that is a palindrome:");
    fgets(str,sizeof(str),stdin);



    temp=palindrome(str);



    system("PAUSE");
    return 0;
    }
    int palindrome(char str[])
    {
    int i, j, retval;

    i = 0;
    j = strlen(str);
    retval = 1;
    while (i <= j)
    {
    --j;
    if (str[i] != str[j])
    {
    retval = 0;
    break;
    }
    i++;
    }
    printf("%d", retval);

    return retval; // return 1 if str is a palindrome, else return 0
    }

    :
    :


  • : :ok i tried using your code and i can only get it to return a 0 and never a 1, i used a little print test statement at hte end too to see what it was returning. can you see a a problem?
    :
    : int palindrome(char str[]);
    : int main()
    : {
    :
    : char str[20];
    :
    : int temp;
    : int retval;
    : printf("enter a string that is a palindrome:");
    : fgets(str,sizeof(str),stdin);
    :
    : [red]cout<<str<<" : is this correct?
    ";[/red]
    :
    : temp=palindrome(str);
    :
    :[red]cout<<"Tem = "<<temp<<"
    ";[/red]
    :
    : system("PAUSE");
    : return 0;
    : }
    : int palindrome(char str[])
    : {
    : int i, j, retval;
    :
    : i = 0;
    : j = strlen(str);
    : retval = 1;
    : while (i <= j)
    : {
    : --j;
    : if (str[i] != str[j])
    : {
    : retval = 0;
    : break;
    : }
    : i++;
    : }
    : printf("%d", retval);
    :
    : return retval; // return 1 if str is a palindrome, else return 0
    : }
    :
    : :
    : :
    :
    :
    Well...it looks to me as if you never have it tell you whether it is a string or not. Maybe you should go through and place "system("PAUSE")" statements at intervals and have it print out the values until then, so that you know where the thing is going wrong. That's what I do for debugging. I randomely have the program stop and tell me what it has, it's actually fairly efficient. and please take note that I don't know how to use "printf"
    [italic]I have studied long and hard. All I learned was that out of all the laws in the universe, one is outstanding and absolute. Nothing is for certain.[/italic]

  • : :
    : :ok i tried using your code and i can only get it to return a 0 and never a 1, i used a little print test statement at hte end too to see what it was returning. can you see a a problem?
    :
    [code]
    int main(int argc, char *argv[])
    {
    int i;

    if (argc < 2)
    {
    printf("
    No string entered
    ");
    return 1;
    }
    i = palindrome(argv[1]);
    printf("
    palindrome returned %d
    ", i);
    return 0;
    }
    [/code]

    It works for me. Are you sure you're entering a palindrome (ie: abcba)?



  • : : : how do i make the compiler recognize if a string is a palindrome or not?

    You can't. The preprocessor is indeed very powerful, but you can't make it do that.


    : :ok i tried using your code and i can only get it to return a 0 and never a 1, i used a little print test statement at hte end too to see what it was returning. can you see a a problem?

    Yes, if you did your own homework, instead of sponging off of other people, you would likely have learned how fgets() works.


    Will
    --
    http://www.tuxedo.org/~esr/faqs/smart-questions.html
    http://www.eskimo.com/~scs/C-faq/top.html
    http://www.parashift.com/c++-faq-lite/
    http://www.accu.org/


  • : : : : how do i make the compiler recognize if a string is a palindrome or not?
    :
    : You can't. The preprocessor is indeed very powerful, but you can't make it do that.
    :
    :
    : : :ok i tried using your code and i can only get it to return a 0 and never a 1, i used a little print test statement at hte end too to see what it was returning. can you see a a problem?
    :
    : Yes, if you did your own homework, instead of sponging off of other people, you would likely have learned how fgets() works.
    :
    :
    : Will
    : --
    : http://www.tuxedo.org/~esr/faqs/smart-questions.html
    : http://www.eskimo.com/~scs/C-faq/top.html
    : http://www.parashift.com/c++-faq-lite/
    : http://www.accu.org/
    :
    :hahaha thanks man, i didn't notice i worded it like that. a sponge hahaha gets me every time.
    :

  • : :hahaha thanks man,

    You're welcome.


    : i didn't notice i worded it like that.

    It would be a lie if I said I was somehow surprised by that.


    : a sponge hahaha gets me every time.

    I'm sure it does, and probably quite frequently.
  • : : :hahaha thanks man,
    :
    : You're welcome.
    :
    :
    : : i didn't notice i worded it like that.
    :
    : It would be a lie if I said I was somehow surprised by that.
    :
    :
    : : a sponge hahaha gets me every time.
    :
    : I'm sure it does, and probably quite frequently.
    :
    Ok, boys and girls, I understand your frustration, but this kind of exchange leads to nothing productive.

    Let's try to solve problems here and forego the temptation to throw barbs at one another, shall we?

    If the code doesn't work for you, try to provide specific information about the nature of the problem; then maybe we can find a solution.


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories