Home > database >  Write a C or a C program that reads 10 characters from the user, and then searches for the positio
Write a C or a C program that reads 10 characters from the user, and then searches for the positio

Time:04-06

Write a C or C program that reads 10 characters from the user, and then searches for the position of the character 'z'.

How would it be done?

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

#define X 100

int main()
{
    char s1[X];
    for(int i = 0; i < 10; i  )
    {
        printf("Char %d = ", i   1);
        scanf("%s", s1);
    }
    
    printf("Char Z index =");
    int index;
    char y = 'z';
    for(int i = 0; i<10; i  )
    {
        if(s1[i] == y)
            index=i;
        // return;
    }
    printf("%d",index);
}

Its supposed to tell me the position of the character z

CodePudding user response:

I don't understand if your problem requires you to take the 10 characters one at a time or all together. If you have to get them all together you could use more simply: gets(s1);. And your compiler may give you an error, as you have defined int i inside the for which you can are only allowed in C99 or C11 mode. Also to check if the z is present inside the string I suggest you do this: if (s1 == 'z').

Furthermore, you cannot input the single characters in that way because every time you enter the next character, the previous one will be automatically deleted and you will reposition the new character in the first position.

Next I advise you to add a flag==0in the for condition, so that once the if condition is true we assign flag=1; and exit the loop without continuing to search for something we have already found.

In summary:

flag=0;
    for(i = 0; i<10&&flag==0; i  ){
        if(s1[i]=='z')
            index=i;
            flag=1;
        // return;
    }
    printf("%d\n",index);

CodePudding user response:

The following C program prompts the user to enter up to N = 10 characters. It reads these characters and stores them in a char array with length N 1 = 11 (the extra character so that the string is null-terminated, which is the kind of string standard library functions typically assume).

It also makes use of an N-element indices boolean array to record if position 0, 1, ..., N of the message has a z or not.

As the program reads a character one at a time, it can modify indices should it read a z.

Finally, it outputs the message entered as well as the positions of the character z.

I hope this helps. Please let me know if there are any questions or if you wanted something else.

#include <stdio.h>
#include <string.h>
#include <stdbool.h> /* C99 or above */

#define N 10

int main(void) {

    int ch, i = 0;
    char message[N   1];
    bool indices[N] = {false};

    printf("Please enter up to 10 characters: ");
    /* read a line of input (max 10 characters) */
    while ((ch = getchar()) != '\n' && ch != EOF)
        if (i < N) {
            if (ch == 'z')
                indices[i] = true;

            message[i  ] = ch;
        }
    /* null terminate the string */ 
    message[i] = '\0';
    printf("The message entered: %s\n", message);

    printf("Positions of character 'z': ");
    for (i = 0; i < N; i  )
        if (indices[i])
            printf("%d, ", i);
    printf("\n");

    return 0;

}

Output:

Please enter up to 10 characters: zgreeziozz
The message entered: zgreeziozz
Positions of character 'z': 0, 5, 8, 9, 
  •  Tags:  
  • c
  • Related