Elgernon
Elgernon

Reputation: 53

Turning a word into *** symbols

So the task is to turn each word in the sentence starting with uppercase into "***".

`

for (i = 1; input[i] != '\0'; i++)
    if(isupper(input[i]))   input[i] = '***';

printf("\n Changed sentence is:     %s", input);

`

This is a code I've written so far. It can change only one character but I don't know how to do it with he whole word.

Upvotes: 0

Views: 214

Answers (5)

Luis Colorado
Luis Colorado

Reputation: 12698

Well, following the feedback seen in the comments, here is my version that doesn't depend on the input line length or memory available to the program. It implements a finite state automaton, three states, to detect first letter, next letters and nonword letters. Following a possible implementation:

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

#define IDLE    (0)
#define FIRST   (1)
#define NEXT    (2)

int main()
{
    int status = IDLE;
    int c;
    while ((c = getchar()) != EOF) {
        if (isupper(c)) {
            switch (status) {
            case IDLE:
                status = FIRST;
                printf("***");
                break;
            case FIRST: case NEXT:
                status = NEXT;
                break;
            } /* switch */
        } else if (islower(c)) {
            switch (status) {
            case IDLE:
                putchar(c);
                break;
            case FIRST: case NEXT:
                status = NEXT;
            } /* switch */
        } else {
            switch (status) {
            case IDLE:
                putchar(c);
                break;
            case FIRST: case NEXT:
                putchar(c);
                status = IDLE;
                break;
            } /* switch */
        } /* if */
    } /* while */
} /* main */

Upvotes: 0

Fiddling Bits
Fiddling Bits

Reputation: 8861

The following is a potential solution:

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

int main(void)
{
    char input[] = "The Mississippi river is very Wide.";

    printf("%s\n", input);

    for(int i = 0; i < strlen(input); i++)
    {
        if((i != 0) && isupper(input[i]))
        {
            printf("*** ");
            while(input[i] != ' ')
                i++;
        }
        else
        {
            printf("%c", input[i]);
        }
    }

    if(ispunct(input[strlen(input) - 1]))
        printf("\b%c\n", input[strlen(input) - 1]);
    else
        printf("\n");

    return 0;
}

Output

$ gcc main.c -o main.exe; ./main.exe
The Mississippi river is very Wide.
The *** river is very ***.

Upvotes: 1

Paolo
Paolo

Reputation: 15847

I assume input is a properly allocated, null terminated C string.


You scan (within the loop) one character at a time; similarly you can change one character at a time.

So you need an additional variable where you store if the word parsed should be converted to a sequence of asterisks.

As you encounter a uppercase letter at the beginning of a word the variable is set to true.

As you encounter the end of the current word (a whitespace) you reset the variable to false.

Finally you change (or not) the current character accordingly.

See the comments in the code

// you need a variable to record if the current word
// should be converted to a sequence of '*'
bool toAsterisk = false;

// sentence scanning loop (one character at a time)
// note that index starts at 0 (not 1)
for (i = 0; input[i] != '\0'; i++)
{
    // check if the current word should be converted to asterisks
    if( isupper( input[i] ) && toAsterisk == false )
    {
        toAsterisk = true;
    }

    // check if you rwach the end of the current word
    if( input[i] == ' ' )
    {
        toAsterisk = true;
    }

    // convert to asterisks?
    if( toAsterisk )
    {
        input[ i ] = '*';
    }
}

Upvotes: 2

Tormund Giantsbane
Tormund Giantsbane

Reputation: 354

Read through the line. If character is capitalized, enter while loop until you get a space or end of line and replacing characters with *.

Print the line.

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

int main()
{
  char line[1024] = "This should Replace capitalized Words!";
  int i = 0;

  printf("line: %s\n", line);

  for (i = 0; i < strlen(line); i++) {
    if (isupper(line[i])) {
      while (line[i] != ' ' && line[i] != '\0' && isalpha(line[i])) {
        line[i] = '*';
        i++;
      }
    }
  }

  printf("line: %s\n", line);
  return 0;
}

Outputs

$ gcc -Wall -Werror test.c -o t
line: This should Replace capitalized Words!
line: **** should ******* capitalized *****!

Upvotes: 1

Krzychu111
Krzychu111

Reputation: 9

By changing one character to three (you change them to three stars) you change length of string. Try starting from there. [This issue may affect the way loop should work]

Upvotes: -1

Related Questions