simple array question

This is a discussion on simple array question within the C Programming forums, part of the General Programming Boards category; hi, i'm trying to search for a character in a string array (see code below), but i'm stuck on how ...

  1. #1
    Unregistered
    Guest

    simple array question

    hi, i'm trying to search for a character in a string array (see code below), but i'm stuck on how to display the character (if found in the string) at its correct position.

    for example, if letter='a' and orgWord="orange", it should display _ _ a _ _ _ .
    Thanks a lot.

    int MatchLetter(char* letter, char* orgWord) {
    char wordStr[50];
    int i;
    char *pdest;
    pdest = strchr( orgWord, letter );

    for (i=0; i<=strlen(orgWord); i++) {
    if (pdest!=NULL) { //letter found in string
    wordStr[i]=letter;
    printf(wordStr[i]);
    return 0;
    }else{
    wordStr[i]='_';
    printf('_');
    return 1;
    } //end if
    } //end else

    } //end function

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    This should work.
    The output is _ _ a _ _ _
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(){
        char array[] = "orange";
        int i = 0;
        while(i < strlen(array)){
            if(array[i] == 'a'){
    	    fputc(array[i], stdout); fputc(' ', stdout);
            }else{
    	    fputc('_', stdout); fputc(' ', stdout);
            }
            i++;
        }
        return 0;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hopefully simple question, input streams
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2006, 12:59 PM
  2. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM
  3. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  4. simple array question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2002, 09:43 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21