user979033
user979033

Reputation: 6480

C language: try to implement IndexOf function

I try to learn C Language and something is not clear to me. I want to write IndexOf function that search for char inside string and return the Index number. I am running this under ubuntu and compile using this:

test1: test.c
    gcc -g -Wall -ansi -pedantic test.c -o myprog1

This is what i have try:

int c;
int result;

    printf("Please enter string: ");
    scanf("%s", str1);

    printf("Please enter char: ");
    scanf("%d", &c);

    result = indexof(str1, c);
    printf("Result value: %d\n", result);

And this is my function:

int indexof(char *str, int c)
{
    int i;
    for (i = 0; i < strlen(str); i++)
    {
        if (str[i] == c)
            return i;
    }

    return -1;
}

So my problem is that my function return -1 all the time

Upvotes: 0

Views: 2097

Answers (3)

alk
alk

Reputation: 70971

write IndexOf function that search for char inside string and return the Index number.

You might like to have a look at the strchr() function, which can be used as shown below:

/* Looks up c in s and  return the 0 based index */
/* or (size_t) -1 on error of if c is not found. */

#include <string.h> /* for strchr() */
#include <errno.h> /* for errno */

size_t index_of(const char * s, char c)
{
  size_t result = (size_t) -1; /* Be pessimistic. */

  if (NULL == s)
  {
    errno = EINVAL;
  }
  else
  {
    char * pc = strchr(s, c);

    if (NULL != pc)
    {
      result = pc - s;
    }
    else
    {
      errno = 0;
    }
  }

  return result;
}

You could call it like this:

size_t index_of(const char *, char);

#include <stdlib.h> /* for EXIT_xxx macros */
#include <stdio.h> /* for fprintf(), perror() */
#include <errno.h>  /* for errno */

int main(void)
{
  result = EXIT_SUCCESS;
  char s[] = "hello, world!";
  char c = 'w';

  size_t index = index_of(s, 'w');
  if ((size_t) -1) == index)
  {
    result = EXIT_FAILURE;

    if (0 == errno) 
    {
      fprintf("Character '%c' not found in '%s'.\n", c, s);  
    }
    else
    {
      perror("index_of() failed");
    }
  }
  else
  {
    fprintf("Character '%c' is at index %zu in '%s'.\n", c, index, s);  
  }

  return result;
}

Upvotes: 0

user2736738
user2736738

Reputation: 30926

scanf("%c",&c)..you are getting input a character.

Working copy would besomethign like:-

char c; // you want to find the character not some integer. 
int result;

printf("Please enter string: ");
scanf("%s", str1);

printf("Please enter char: ");
scanf("%d", &c);

result = indexof(str1, c);
printf("Result value: %d\n", result);

Working example:-

#include <stdio.h>
#include <string.h>
 int indexof(char *str, char c)
    {
        int i;
        for (i = 0; i < strlen(str); i++)
        {
            if (str[i] == c)
                return i;
        }

        return -1;
    }

int main()
{
    char c;
    int result;
    char str1[100];

        printf("Please enter string: ");
        scanf("%s", str1);

        printf("Please enter char: ");
        scanf(" %c", &c);

        result = indexof(str1, c);
        printf("Result value: %d\n", result);

}

Upvotes: 1

wrangler
wrangler

Reputation: 3576

Since c is int:

int indexof(char *str, int c)
{
    int i;
    for (i = 0; i < strlen(str); i++)
    {
        if ((str[i]-'0') == c) // convert char to int
            return i;
    }

    return -1;
}

Upvotes: 0

Related Questions