Ivan Vulović
Ivan Vulović

Reputation: 2214

Assign value to pointer in C

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. :(

Upvotes: 2

Views: 126

Answers (1)

unwind
unwind

Reputation: 400029

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>.

Upvotes: 8

Related Questions