2

I am having problem in pset2 initials i am getting only my first letter initialized infinitely .so, i need to know the problem to fix the errors.

{    
    // asks user for the name

    string s = GetString();

    for(int i = 0,n = strlen(s); i < n;i++)
    {
        if (islower(s[i = 0]))
        {
            printf("%c",toupper (s[i = 0]));
        }
            else
        {
            printf("%c",s[i = 0]);
        }
    }
    printf("\n");
 }
0

3 Answers 3

0

It is because in your inlower function you set array's index always to zero, in your printf functions you need to write tupper(s[i]) since you are already initialized your i = 0 in the loop and it will increment every time to print all the characters. Same as for you else statement write down only s[i]. Hope that make sense :).

2
  • Then what should i do to solve the problem.,bro. Commented Nov 12, 2015 at 4:09
  • well i just gave the answer above, instead of s[i = 0], just have s[i] Commented Nov 20, 2015 at 16:57
0

In this problem you have to print only those letters which are the first letters of the first name or the middle name or the last name.

So, the first letter to appear in string s must be printed and any letter which is preceded by a space must be printed.

The question also requires you to print in upper case.

You can try this as your code inside for loop

{
    if (i == 0 )
       if (isalpha(s[i]))
          printf ( "%c", toupper(s[i]) );

    else
       if (s[i - 1] == ' ' && isalpha(s[i]) )
          printf ( "%c", toupper(s[i]) );
}
0

This is nothing but a syntax error, You have typed isupper(s[i = 0]) in your if condition as well as at other places. This initiliase your string to first character always by assigning i, a value of 0 and since any number, character is true in if, you get the error, instead of getting it checked by if.

Better use this syntax:

s[i]==0 in your conditions like if()

and

s[i] in functions like isupper(), toupper() and printf()..

Hopefully this will resolve your problem!

Not the answer you're looking for? Browse other questions tagged .