I know I would first declare the input variable as char. But that just reads in the first letter of the word.
How can I print or read each letter of a certain word?

try this

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

int main(void)
{
    char myString[32];
    int length, i;

    strcpy(myString,"Hello World!");
    length = strlen(myString);

    for (i=0; i<length; i++)
    {
       printf("index %02d :   %c    0x%02x\n", i, myString[i], myString[i]);
    }

    return 0;
}

.

Edited 6 Years Ago by jephthah: n/a

Character array.

Is there any other way of doing it without the use of arrays?

try this:

int main(void)
{
    const char constString[32] = "Hello World!";
    char *myPtr = &constString;

    while(*myPtr != '\0')
    {
       printf("address 0x%p:   %c    0x%02x\n", myPtr, *myPtr, *myPtr);
       myPtr++;
    }

    return 0;
}

I created a program like this

#include <stdio.h>

main()
{
 /* Declare variables */
 char word[] = " ";

        printf("Enter a word: ");
        scanf("%s", &word);

        printf("The word entered is %s\n", word);
}

It compiles and runs as I had intended.
But now how would I go about reading and printing each character of the input word?

Is there any other way of doing it without the use of arrays?

In the context you've mentioned a word is an array of characters. And since you're dealing with an array of characters--i.e. a word as you put it--you must deal with the 'word' as an array.

There are probably an endless amount of ways to deal with your situation as there are programmers, but when you get right down to it, you have to deal with the word as an array.

Because that's what it is.

char word[] = " ";

In this case you could declare an integer i and loop through the array of the string entered.

int i ;

....

for (i=0; word[i]!=NULL; i++)
{
    printf("%c ", word[i]) ;
}

Edited 6 Years Ago by hkdani: n/a

I created a program like this

#include <stdio.h>

main()
{
 /* Declare variables */
 char word[] = " ";

        printf("Enter a word: ");
        scanf("%s", &word);

        printf("The word entered is %s\n", word);
}

It compiles and runs as I had intended.

Then you're very lucky. Almost any input you use will blow out your array, overwriting memory you don't want to touch. char word[] = " "; defines an array of 2 characters (one SPACE, one '\0'). Enter 3 characters and you overwrite data that's not part of the array.
You need to specify a value for the array size: char word[120] = " "; which means you can enter up to 119 characters followed by the necessary '\0'.

But now how would I go about reading and printing each character of the input word?

Depends on if you want to output one character at a time, or the whole word at once. Please think about how to do either.

Depends on if you want to output one character at a time, or the whole word at once. Please think about how to do either.

Alright, so I changed my character array to read up to 50 characters.

#include <stdio.h>

main()
{
 /* Declare variables */
 char word[50] = " ";

        printf("Enter a word: ");
        scanf("%s", &word);

        printf("The word entered is %s\n", word);
}

I would like to read and print each character of the word, one at a time. I have a function that determines whether or not a character belongs to the Hawaiian dictionary. The function determines the appropriate vowels and consonants for a word to belong to the Hawaiian dictionary. So it would return either a TRUE for yes, or FALSE for no.
So basically, my goal is to create some sort of Hawaiian spell checker program.

Here is my function:

int is_vowel(char c)
/*  This function is given a character and returns true if it is
 *          a valid vowel, false otherwise.  */

{
        /*  test the character  */

        switch(c)
        {  case 'a':
           case 'e':
           case 'i':
           case 'o':
           case 'u':
           case 'A':
           case 'E':
           case 'I':
           case 'O':
           case 'U':  return TRUE;
           default :  return FALSE;
        }
}


int is_h_consonant(char c)
/*  This function is given a character and returns true if it is
 *          a valid hawaiian consonant, false otherwise.  */

{
        /*  test the character  */

        switch(c)
        {  case 'h':
           case 'k':
           case 'l':
           case 'm':
           case 'n':
           case 'p':
           case 'w':
           case 'H':
           case 'K':
           case 'L':
           case 'M':
           case 'N':
           case 'P':
           case 'W':
           case '`':  return TRUE;
           default :  return FALSE;
        }

}

I want to read each character of the given word, and assign it as a parameter of both functions above.
I am still a newbie when it comes to characters, so any help would be really appreciated?

Edited 6 Years Ago by tquiva: n/a

But now how would I go about reading and printing each character of the input word?

i handed you two scenarios to do exactly that.

If you can't bother to read the answers in your own thread, i have no interest in helping you any further.

I want to read each character of the given word, and assign it as a parameter of the function above.
I am still a newbie when it comes to characters, so any help would be really appreciated?

I would combine both of your functions into one to keep it simple. Don't see the need to break it down into two functions.

If you declare functions at top, then you don't have to prototype them.

int i,  j ;

for (i=0 ; word[i]; i++)
{
     j = is_whatever (word[i]);
   // Test the return value
   if (j ==FALSE)
           // Code to deal with a false return
           // You might want to write code to break out of the loop here
    else
           // Code
 }
  return j; // ?

you can loop through the word as mentioned above

I would combine both of your functions into one to keep it simple. Don't see the need to break it down into two functions.

If you declare functions at top, then you don't have to prototype them.

int i,  j ;

for (i=0 ; word[i]; i++)
{
     j = is_whatever (word[i]);
   // Test the return value
   if (j ==FALSE)
           // Code to deal with a false return
           // You might want to write code to break out of the loop here
    else
           // Code
 }
  return j; // ?

you can loop through the word as mentioned above

much thanks to hkdani and jephthah for the advice.

#include <stdio.h>
#include "letters.h"

main()
{
 /* Declare variables */
 char word[50] = " ";
 int i, j;

        printf("Enter a word: ");
        scanf("%s", &word);
        printf("The word entered is %s\n", word);

        for(i == 0; word[i]; i++)
        {
          j = is_vowel(word[i]);

          /* Test return value */
          if (j == False)
          {
           printf("not a Hawaiian word\n");
          }
          else
          {
           printf("is a vowel\n");
          }
        }

}

I edited my program, but now it can't compile due to line 21.

This is the error I received:
test.c: In function âmainâ:
test.c:21: error: âFalseâ undeclared (first use in this function)
test.c:21: error: (Each undeclared identifier is reported only once
test.c:21: error: for each function it appears in.)

Edited 6 Years Ago by tquiva: n/a

this is the most current one I'm working with

#include "letters.h"
#define FALSE 0
#define TRUE 1

/* Declare function */
int is_vowel(char c);

main()
{
 /* Declare variables */
 char word[50] = " ";
 int i, j;

        printf("Enter a word: ");
        scanf("%s", &word);
        printf("The word entered is %s\n", word);

        for(i == 0; word[i]; i++)
        {
          j = is_vowel(word[i]);

          /* Test return value */
          if (j == FALSE)
          {
           printf("not a Hawaiian word\n");
          }
          else
          {
           printf("is a vowel\n");
          }
        }

}

int is_vowel(char c)
/*  This function is given a character and returns true if it is
 *   *  a valid vowel, false otherwise.  */

{
        /*  test the character  */

        switch(c)
        {  case 'a':
           case 'e':
           case 'i':
           case 'o':
           case 'u':
           case 'A':
           case 'E':
           case 'I':
           case 'O':
           case 'U':  return TRUE;
           default :  return FALSE;
        }
}

I get a segmentation fault when I run it ..

for(i == 0; word[i]; i++) better for (i = 0; word[i] != '\0'; i++) char word[50] = " "; better char word[50] = { '\0' }; scanf("%s", &word); better scanf("%49s", word) Ponder about why.

Edited 6 Years Ago by Aia: n/a

for (i=0 ; word[i]!=NULL; i++) My bad. The second portion of this type of for loop is a conditional test. Left out the test.

This article has been dead for over six months. Start a new discussion instead.