1

I have programmed a Programm that reverses me every Word in an array but my problem is i wanna have the first Letter of a Word in uppercases and all the other in lowercase.

C

#include <stdio.h>
#include<stdlib.h>
#include<ctype.h>

char * reversePrint( char *name )
{
    char *input_string = name;
    char temp;

    while (*input_string)
    {
        char *t = input_string;
        /* End of non Whitespace sequence */
        while (*t && *t == (unsigned char)*t && !isspace(*t))
        {
            t++;
        }
        if (t - input_string > 1)
        {
             /* Non whitespace Sequence  >1*/
            char *e = t;

            /* Reverse Words */
            do
            {
char temp = *input_string;

                *input_string++ = *--e;
                *e = temp;
            } 
        while (input_string < e);

//Paste reversed Sequence
            input_string = t;
        }
        else
        {
            //Non whitepace skip
            input_string++;
        }
    }

    return name;
}

int main( void ) 
{
    char s[] = "I love Pizza";

    printf("%s", reversePrint(s));

    return 0;
}

So my Problem is i get as a Result I Evol azziP but i have to have I evol Azzip

Can anyone help me out?

  • Shouldn't be **I Evol Azzip** – LPs Dec 05 '19 at 10:44
  • Why does "evol" not need to have an uppercase letter? – kaylum Dec 05 '19 at 10:44
  • Does this answer your question? [Converting Char \* to Uppercase in C](https://stackoverflow.com/questions/35181913/converting-char-to-uppercase-in-c) and obviously there is `tolower` function – LPs Dec 05 '19 at 10:46
  • Yes but when i reverse the word and wanna change the first and the last character i got a seg fault –  Dec 05 '19 at 10:50

1 Answers1

1

You can use something like the following which:

  • make to upper case all first char of a word.
  • make to lower case all the others

I have made the code verbose so it is easier for you to understand what is going on. It can be refactored into much more compact code.

    void make_upper(char* line)
    {
        bool first = true;
        while(*line)
        {
            if(*line != ' ' && !first)
            {
                *line = tolower(*line);
            }
            //non first char of a word
            if(*line != ' ' && first)
            {
                *line = toupper(*line);
                first = false;
            }
            //new word ahead       
            if(*line == ' ')
                first = true;
            line++;        
        }    
    }

which in produces: Nomis Tbeil !azzip

LIVE DEMO


As a side note, Reverse print function is extremely hard to read. Don't write long and complicated functions but instead, write small and reusable functions and build complex features by using those small blocks together.

Can you imagine a car manufacturer assembling all pieces of the machine in one go? can you imagine assembling the pistons or the electronics where the chassis is assembled? Would be a huge mess. The engine is assembled separately and only when complete is put together with the rest of the car. Do the same with software.

Community
  • 1
  • 1
Davide Spataro
  • 7,319
  • 1
  • 24
  • 36