logo
down
shadow

Why doesn't my code for checking if a word is a palindrome work?


Why doesn't my code for checking if a word is a palindrome work?

By : Ganiu Hassan
Date : November 21 2020, 03:00 PM
To fix the issue you can do The actual problem was failing to remove the trailing newline. It sometimes helps to quote your output for easy recognition of non-printing character problems like tabs, newlines, etc.. Doing so immediately reveals the problem. (note: where the quotes end up with your original logic)
code :
$ ./bin/palindrome
foo
The word contains 3 lettersThe word contains 3 letters
The word reversed is '
oof' <-> 'foo
' (original) are not equal
#include <stdio.h>
#include <string.h>

#define STRING_LENGTH 200

int main() {

    int x = 0, Ordlengde;
    char s[STRING_LENGTH] = "",
        palindrom[STRING_LENGTH] = ""; 

    printf ("enter a word: ");
    if (!fgets (s, STRING_LENGTH, stdin)) { /* VALIDATE INPUT */
        fprintf (stderr, "error: invalid input - EOF.\n");
        return 1;
    }

    Ordlengde = strlen(s) - 1;
    if (s[Ordlengde] == '\n')               /* check/remove '\n' */
        s[Ordlengde] = 0;

    while (Ordlengde--)                     /* reverse s */
        palindrom[x++] = s[Ordlengde];

    printf ("\noriginal: '%s'\nreversed: '%s' - ", s, palindrom);

    if (strcmp(s , palindrom) == 0)         /* compare */
        printf("is a palindrom\n");
    else
        printf("is not a palindrom\n");

    return 0;
}
$ ./bin/palindrome
enter a word: foo

original: 'foo'
reversed: 'oof' - is not a palindrom

$ ./bin/palindrome
enter a word: foof

original: 'foof'
reversed: 'foof' - is a palindrom
printf ("enter a word: ");
Ordlengde = strlen(s);
if (Ordlengde && s[Ordlengde - 1] == '\n')  /* remove '\n' */
    s[--Ordlengde] = 0;


Share : facebook icon twitter icon
Checking If a String Contains a Word and Then Checking If It Starts With said Word and If It Doesnt?

Checking If a String Contains a Word and Then Checking If It Starts With said Word and If It Doesnt?


By : Abinav Prabhakar
Date : March 29 2020, 07:55 AM
I wish this help you Having another tough one here guys, basically I would like to check if a string contains the word "Foo" and if it does contain it, does it start with it? If it does start with Foo, it should be the only Foo that starts with a capital, all others should be small letters. , If I undestood it correctly this should do it !
code :
    if (testString.IndexOf("Foo") == 0 && testString.LastIndexOf("Foo") == 0)
        // "Foo foo foo"
        return true;
    else if (testString.IndexOf("Foo") == 0 && testString.LastIndexOf("Foo") > 0)
        // "Foo foo Foo"
        return false;
    else if (testString.Contains("foo")  && testString.IndexOf("Foo") > 0)
        // "foo Foo foo" or "foo foo Foo"
        return false; 
    else if (testString.Contains("foo") && !testString.Contains("Foo"))
        // "foo foo foo"
        return true;  
Checking if a word is a Palindrome or not

Checking if a word is a Palindrome or not


By : partha
Date : March 29 2020, 07:55 AM
I wish this helpful for you Comparing two distinct StringBuilder instances with == would always give you false, regardless of their content, since they are not the same instance.
Try revStr.toString().equals(str)
code :
public static boolean isPalindrome(String s) {
    return new StringBuilder(s.toLowerCase()).reverse().toString().equals(s.toLowerCase());
}
How does this line of code for checking whether a word is palindrome or not, work in JS?

How does this line of code for checking whether a word is palindrome or not, work in JS?


By : whalesmoker
Date : March 29 2020, 07:55 AM
it fixes the issue tagged template literals: without tagged template literals the sample would look like this:
code :
p = s=>s==[...s].reverse().join('')
tagFunction`literal ${placeholder} template`
Checking if a word is a palindrome

Checking if a word is a palindrome


By : user3267223
Date : March 29 2020, 07:55 AM
should help you out According to this demo, your code is running fine - however, I noticed your input statement doesn't have a space after it. Are you typing a space before you put your word in? If so, consider the strip() function, which will remove leading and trailing spaces - or just add a space to your input prompt!
PEP-8 - Checking to See if Word is a Palindrome

PEP-8 - Checking to See if Word is a Palindrome


By : Yacov
Date : March 29 2020, 07:55 AM
I hope this helps you . I have the input sorted out for the palindrome program but I haven't got the slightest idea of how to check whether the inputted word is a palindrome or not. , Here's some simple C-ish pseudo-code to get you started:
Related Posts Related Posts :
  • Signal SIGSEGV recieved: vfprintf.c: No such file or directory?
  • Does Posix thread ID have an one-to-one relation with linux thread ID?
  • C - fgets doesnt wait for input when initializing pointer
  • I cannot perform data validation on my four arrays properly.
  • How do I fix this segmentation fault in my c code
  • How to use pointer to split the string into two strings? C language
  • Knuth List Insertion Method in C
  • Tons of error in Visual Studio 2017 with GetUserNameEx at compile time
  • Invalid conversion of char to float, different codes and no good results
  • My program compiles and when ran, it doesn't give me the input i put in
  • How to find if a graph contains a cycle using a recursive DFS?
  • Memory mapped file cannot be closed without un-mapping, since it's still referenced
  • Why is this simple program giving me seemingly incorrect output?
  • second printf not working when using a variable C-programming
  • Shared memory variable in c
  • Linked List and Pointers in C
  • Counting Character usage in text file? C
  • C: Why does assigning a 2D array of ints to an int** cause CLION to highlight the line?
  • How does fwrite work?
  • Why is my http server printing out the same bytes? (C)
  • The following code doesn't stop looping
  • Strcmp gives segmentation Fault
  • Segmentation fault: 1902 vfscanf.c: No such file or directory
  • Mutex - counting occurrences of a char in files using threads
  • Returning arrays in C instead of switch statements, nested set of values
  • Trying to pass addresses to simple variables to a function (pointers & loops) In C
  • Recursively list directories in C on Linux
  • Math operation in the test expression of the `for` loop - perfomance, optimisation
  • what is the difference between extern char **environ and extern char *environ[]
  • Is it possible to bind a socket to 2 adresses in c?
  • error when compiling testfiles from installed c-algorithms library
  • Count alternating up / down sequences
  • ISR documentation with doxygen
  • How to solve the sum of three digit numbers,with how to calculate the second number
  • Recursion C image compressor algorithm
  • Unix system programming: get a network identifier to be passed to getaddrinfo
  • misaligned address access crash on linux wifi drivers on arc platform
  • Having a character appear repeated times in an array and score counts.
  • Given a number A(=2^N), how to get the N?
  • Reading Hex from an file
  • Realloc affecting fgets
  • Run C server and client files in CLion at the same time
  • How to check last character in command line arguements?
  • How to seperate user input word delimiter as space using strtok
  • Code to add element to the beginning of a list in c - what's the error?
  • assembly output + questions about stacks
  • memory management for a dynamically allocated 2D space
  • Don't get messages with epgm://, while with tcp:// do. Why?
  • C Recursion segmentation fault
  • struct dirent in C d_name[256] and NAME_MAX definition
  • Defining a symbolic constant for GNU .ld script origin
  • Bitwise operations in C coding
  • my open gl doesn't work properly?
  • Could not understand the output from the code
  • How can I count how many times this loop has been ran after it finishes?
  • Converting C code to x86-64 assembly
  • Segmentation fault upon reading from an array
  • const char **p comparison between pointer and integer if (**p == NULL)
  • Are C unions never padded at the beginning?
  • finding subsets dicretely c
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org