2

I need to place * on every lowercase letter, but my program blocks always. Although this seems like a simple problem I cant find simple solution. Please help.

#include <stdio.h> 

void f(char *p)
{
    int i = 0;
    char c = '*';
    while(p[i] != '\0')
    {
        if(p[i]> 96 && p[i] < 122 )
        {
            p[i] = c; # here program block
        }
        i++;
    }
    printf("%c",p);
}

int main(void)
{
    f("tesT");
    return 1;
}

I found some similar problems on internet but without success. :(

Ivan Vulović
  • 2,147
  • 6
  • 27
  • 43
  • This question is asked very frequently. [Please read any C FAQ.](http://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-string) – Lundin Jun 24 '13 at 15:07

1 Answers1

8

You cannot modify string literals.

Try:

int main(void)
{
   char buf[] = "tesT";

   f(buf);

   return 1;
}

also, never hardcode ASCII values, use islower() from <ctype.h>.

unwind
  • 391,730
  • 64
  • 469
  • 606