can anyone help me make my program work. after it capitalize i want it to reverse the new string. something like palindrome.

#include<stdio.h>
#include<ctype.h>
#include<string.h>
/*program begins here*/
int main()
{
    /**/
    char string[100];
    char *ptr = string;
/*shws the user what to do*/
    printf("Enter string and use a space after a fullstop\n");
/*gets the string that was entered*/
    gets(string);

/* looks at the first letter of string */
    *ptr = toupper(*ptr);
    
    /* going through the string entered*/
    while(*ptr)
    {
        if( isspace(*ptr) )
        {
            *(++ptr) = toupper(*ptr);
            
        }
        ++ptr;
    }
/*shows user the new string*/
    printf("your new string is %s\n",string);


}
// Reverse string function
int ReverseString(char *str)
{
     int x = strlen(str); //takes length of string
     for(int y = x; y >= (x/2)+1; y--) //for loop arranging the string
     {
          swap(str[x-y],str[y-1]); // swaps letters
     }
     return 0; //return
}

You neglected to explain what the problem is.

You neglected to explain what the problem is.

the problem is as stated below

You may be familiar with palindromes... This is a word or phrase which is the same whether viewed as is or reversed.

For example:
Ada, is the same viewed either in normal direction or reversed.
Also, Madam, I'm Adam. is the same in either direction as well.
There are some things one has to consider...
Ignore capitalization (change all characters to uppercase, or lowercase.)
Ignore punctuation.
Ignore whitespace characters.
Problem:


You are to define a function int IsPalindromic(char * string) which will return a true value(1) when the passed in string parameter is palindromic, otherwise it will return a false value (0.)

You will test this with the above examples to see if it works (also some other which are not palindromic.)

Then using the following file: words
You will read in each word, and output the word only when it is palindromic.

your "ReverseLetters" function doesnt work.

You had some other problems as well.

ive outlined one way you can approach this. now you need to (1) fix the "reverseLetters" function, then (2) write the palindrome detector.

#include<stdio.h>
#include<string.h>

void swap(int i, int j)
{
    int t;

    t=i;
    i=j;
    j=t;
}

int reverseString(char *str)
{
    int x = strlen(str); 
    int y;
    
    for(y=x; y >= (x/2)+1; y--) 
    {
        swap(str[x-y],str[y-1]);
    }
    return 0; 
}

void removeNonAlpha(char *str)
{
    char *str_ptr = str;
    int index=0;
    
    while(*str_ptr)
    {    
        if ((*str_ptr > 0x40 && *str_ptr < 0x5B) || 
                    (*str_ptr > 0x60 && *str_ptr < 0x7B))
            str[index++] = *str_ptr++;
        else
            str_ptr++;
    }
    str[index] = 0;
}
    
int printAllPalindromes(char *str)
{
    char origStr[100];
    
    strcpy(origStr,str);
    
    reverseString(str);
    printf("reversed letters : %s\n",str); 
       
    // use origStr and str to find palindromes
    
    printf("\nFound Palindromes: ????\n");
    return 0;
}
    
    
int main()
{

    char str[100];
    char *ptr;

    printf("Enter text string: ");
    fgets(str,99,stdin);
    
    removeNonAlpha(str);
    printf("removed non alpha: %s\n",str);
        
    ptr=str;
    while (*ptr) {
        *ptr = tolower(*ptr);
        *ptr++;
    }
    printf("changed to lower : %s\n",str);    
        
    printAllPalindromes(str);

}

another suggestion: don't use comments to explain what standard C library functions do. its annoying. everyone knows what a "for" loop does, what a "printf" statement does, and what a "return" statement does.

>ive outlined one way you can approach this.
Sloppy. Did you even test it?

>void swap(int i, int j)
Everything is passed by value in C, which means that your swap function swaps copies and leaves the originals the way they were. You need to pass pointers to access the original values:

void swap ( char *a, char *b )
{
  char save = *a;
  *a = *b;
  *b = save;
}
for(y=x; y >= (x/2)+1; y--) 
{
  swap(&str[x-y],&str[y-1]);
}

That's overly complicated. You already have two iterators, so there's no reason not to use them in an intuitive way (x starts at 0, y starts at strlen ( str ) - 1, and you move them closer until they meet):

char *reverseString ( char *str )
{
  int x = 0;
  int y = strlen ( str ) - 1;

  while ( x < y )
    swap ( &str[x++], &str[y--] );

  return str; 
}

Also, returning 0 solves nothing, so why not simply return the string itself so that this function can be used in an expression?

>if ((*str_ptr > 0x40 && *str_ptr < 0x5B) ||
>(*str_ptr > 0x60 && *str_ptr < 0x7B))
Ever heard of isalpha? It's a useful function that saves you from using magic numbers and assuming non-portable relationships between the characters in the basic character set.

>fgets(str,99,stdin);
fgets already makes room for the null character. You don't have to subtract one from the size.

You forgot to include <ctype.h>, and you also forgot to return a value from main. That's currently not a recommended practice because it's only valid in C99 programs, and C99 isn't widely implemented and used just yet.

>everyone knows what a "for" loop does, what a "printf"
>statement does, and what a "return" statement does.
I find this humorously ironic.

i did test it, yes, and it worked.

a lot of your critiques were what the OP already had in there, and i didnt bother changing.

but you're right. theres some sloppy stuff. still, i say "sloppy working" is better than "sloppy not-working" which is what it was when i found it.

i've lost enthusiam i might have once had to try and make bulletproof and pretty code for these CSC 101 class assignments.

i'm also, sadly, not a member of the Beautiful Code Club :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.