#Replacing in a string

10 messages · Page 1 of 1 (latest)

coarse condor
#

I need some help with this one.. I can't find anything similar to this exercise online. I've read the chapter about arrays in my coursebook and watched several youtube tutorials but I just can't figure this one out.

It's pretty embarrasing but here's one of my tries thus far, I know there's probably many errors but every time I try to change anything it seems to get worse.


int main()
{
    char s[61];
    int n;
    char c;

    while (scanf("%60s", s)==1)
    {
        for (int i=0; i<n; i++)
        {
            scanf("%d", &n[i]);
        }
        scanf("%c", &c);
        printf("%c\n", s[c]);
    }
}
indigo compassBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

coarse condor
#

the %c in my printf will only print a single char, right? So how do I get it to print a string as output? Should the printf be located inside the for loop instead?

quartz jasper
#

%c is a single character yes

#

you can print a string with %s

#

&n[i] is an error

coarse condor
#

yeah but in my case, replacing %c with %s the compiler would complain and ask me to change it back to %c.

#

I think I got it to work now, removed the for loop and added str[n]=c

#

int main()
{
    char c;
    int i;
    char str[60];

    while (scanf("%59s %d %c", str, &i, &c)==3)
    {
        str[i]=c;
        printf("%s\n", str);
        
    }
}
coarse condor
#

!solved