0

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;
}
3
Contributors
2
Replies
26
Views
2 Years
Discussion Span
Last Post by TalhaMoazSarwar
0
#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;
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.