Thread: case sensitive and spacing

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    27

    case sensitive and spacing

    Code:
    #include <stdio.h>
    #include <string.h>
    int main(){
    char string1[20];
    int i, length;
    int flag = 0;
    printf("Enter a string: ");
    scanf("%s", string1);
    length = strlen(string1);
    
    for(i=0;i < length ;i++){
    if(string1[i] != string1[length-i-1]){
    flag = 1;
    break; } }
    
    if (flag) {
    printf("%s is not a palindrome ", string1); }
    else {
    printf("%s is a palindrome ", string1); }
    
    return 0;
     }
    


    Hey guys, I have a program that takes a string and says if it is a palindrome or not. Right now it is case sensitive (Pop would not count as a palindrome) and it also disregards spacing (p o p is a palindrome). How do I change it so that Pop WOULD count as a palindrome and p o p WOULD NOT count as one? Pretty much what do I need to do to make it case insensitive and also make it so that if there are spaces, it does not count as a palindrome? Thanks for any help.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Read about tolower and isspace
    tolower - C++ Reference
    isspace - C++ Reference

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    27
    can those be used with C as well?

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Yes, just remove the c from the library name and add .h

    So, include <ctype.h> -- not <cctype>

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It never fails to amaze me how people continue to explore the possibilities of all the useless tags that can be applied to code.
    Another eyesore post that's far too easy to ignore.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Case sensitive
    By soled_boy in forum C++ Programming
    Replies: 1
    Last Post: 02-02-2009, 02:45 PM
  2. program is case-sensitive
    By dra in forum C++ Programming
    Replies: 8
    Last Post: 05-02-2005, 10:40 AM
  3. Comparing a string (case sensitive)
    By Waspntr in forum C++ Programming
    Replies: 9
    Last Post: 02-06-2003, 02:35 PM
  4. case sensitive
    By GanglyLamb in forum C Programming
    Replies: 3
    Last Post: 11-09-2002, 04:25 AM
  5. Case Sensitive
    By JamMan in forum C++ Programming
    Replies: 1
    Last Post: 11-19-2001, 05:48 PM