0

Need help in converting a example string like this:
Example String
into:
eXaMpLe StRiNg
by preserving the whitespaces In C Programming language. I referred to other posts, but didn't find any help for writing in C.

I tried using this logic:

#include<stdio.h>
#include<string.h>
int main()
{
    int i,len;
    char a[30],b[30];

    printf("Enter a string\n");
    gets(a);
    len=strlen(a);
    for(i=0;i<len;i=i+2)
    {

        if(a[i]!=' ' && isalpha(a[i]))
        {
            a[i]=tolower(a[i]);
        }
        else if(a[i]!=' ')
            a[i]=toupper(a[i]);
    }
    printf("New string is %s\n",a);
    return 0;
}

  • 1
    What kind of help did you need? You should design an algorithm first. Once you have that the language is just a tool to put it into action (C or otherwise). If you're asking us to do your homework for you, you're on the wrong site. – WhozCraig Nov 21 '20 at 08:26
  • Sorry, let me post a sample code i used now. – Pooja P Nadiger Nov 21 '20 at 08:26
  • [Update your question](https://stackoverflow.com/posts/64941099/edit) to include that code, and explain what problems you seem to be having, including the actual input and resulting output from your tests. – WhozCraig Nov 21 '20 at 08:28
  • Done, updated the questions with code and sample input and output. Please help me in getting the alternate casing – Pooja P Nadiger Nov 21 '20 at 08:31
  • You forgot the *actual* output part of your code. And ideally it's a [mcve] (you're missing headers, a `main`, variable declarations, etc.) – WhozCraig Nov 21 '20 at 08:33
  • Ok, let me upload complete code now – Pooja P Nadiger Nov 21 '20 at 08:34
  • Never ***ever*** use `gets`! It's such a [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) function it has even been removed from the C standard. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Nov 21 '20 at 08:34
  • Sure, please help me in loop iteration for getting alternative casing on string – Pooja P Nadiger Nov 21 '20 at 08:35
  • As for your problem, you need to modify *all* letters, right now you're skipping every second letter and leave its casing as it is. You also need to keep a state (a variable) for the last letter, if it was upper or lower case. – Some programmer dude Nov 21 '20 at 08:36
  • Sorry didn't get your answer. Please post a sample code inside the for loop with changes. – Pooja P Nadiger Nov 21 '20 at 08:37
  • The example you provided does not have white spaces, post an example with white spaces or multiple words – IrAM Nov 21 '20 at 08:39
  • @IrAM How does `Example String` *not* have white spaces? – WhozCraig Nov 21 '20 at 08:45
  • @WhozCraig Sorry, I thought `Example` is not part of `Example String`. – IrAM Nov 21 '20 at 08:53
  • @IrAM the followup conversion result, `eXaMpLe StRiNg` was the dead give-away to me. – WhozCraig Nov 21 '20 at 08:55

1 Answers1

1

First of all, though it is unrelated to your problem, you shouldn't use gets ().

Then, what you need is just to tolower() the even elements of the string and to toupper () the odd elements. We need to process all elements (you used to process just one every two elements).

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

int main()
{
    int i,len, alphas;
    char a[30],b[30];

    printf("Enter a string\n");
    fgets(a, 30, stdin);
    len=strlen(a);

    for(i=0, alphas=0;i<len;i++)
    {
        if (isalpha(a[i]))
        {
            if(alpha%2 == 0)
            {
                a[i]=tolower(a[i]);
            }
            else
                a[i]=toupper(a[i]);

            alphas++;
        }
    }
}

We need to make sure that spaces don't count in our sequence, so we keep a variable alphas counting alphabetical chars. We understand if an index is even with modulus operator %, applied to our counter alphas.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39