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;
}

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.