Hi. How to find index of first letter in every word in a string?
For example, input string is: Programming Languages. Output should be: 0 12, those are the indexes of letters "P" and "L".
I tried different approach, to print those numbers but that didn work. Thanks for the answers.

Code:

#include<stdio.h>
#include<string.h>
int main()
{
char str[100];int i;int n=strlen(str);
printf("enter a string:\n");
gets(str);
for(i=0;i<n;i++)
{
    if(str[i]==' ')
    {
        printf("%c",str[i+1]);
    }
    else
        printf("%c",str[i]);
}
return 0;
}

Dani AI

Generated

Common problems in the posted code: strlen(str) is called before the buffer is populated and gets() is used (both cause undefined/unsafe behavior). 's split-into-words idea is valid but allocates extra buffers; 's solution works but uses goto where a simple scan is clearer. The following is a compact, safe scan that prints the zero-based index of the first alphabetic character of each word (treats punctuation as a separator).

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

int main(void) {
    char s[256];
    if (!fgets(s, sizeof s, stdin)) return 0;
    size_t len = strlen(s);
    if (len && s[len-1] == '\n') s[--len] = '\0';

    for (size_t i = 0; i < len; ++i) {
        if (isalpha((unsigned char)s[i]) &&
            (i == 0 || !isalpha((unsigned char)s[i-1]))) {
            printf("%zu ", i);
        }
    }
    putchar('\n');
    return 0;
}

Notes and edge cases:

  • The code uses isalpha so the first alphabetic character in a token is reported. If the goal is "first non-space character" instead, replace isalpha(...) with !isspace((unsigned char)s[i]).
  • Output is zero-based (the sample "Programming Languages." prints 0 12). Use i+1 to produce one-based indices.
  • This counts bytes in the C string; for UTF-8 multibyte characters the index will be a byte offset, not a Unicode code point. Proper Unicode character indexing requires decoding UTF-8 or using a library.
  • This is a single-pass O(n) scan and avoids the unsafe gets() and the pre-read strlen() bug seen in 's post. Splitting into words (as suggested) is fine when tokens are needed; scanning in-place is simpler and faster when only indices are required.

Recommended Answers

All 2 Replies

I'd advice you to split the sentance to words and save the words into an array. Then just print the first char of each string that is in the array

This is an example of splitting

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

main()
{
    char str[100];
    int i, len, f = 0;
    printf("Enter a string: ");
    gets(str);
    len = strlen(str);
    printf("The indices are: ");
    Again: // Loop while character not found in start //
    if (str[f] != ' ')
    {
        printf("%c = %d", str[f], f);
    }
    else
    {
        f++;
        goto Again;
    }
    for (i = f; i < len; i++)
    {
        if (str[i] == ' ')
        {
            printf(", %c = %d", str[i+1], i+1);
        }
    }
    getchar();
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.