Maybie you could suggest how to solve my problem. I'm trying to do something like this. I have 2 strings. In one of these i have a text with many words in other one I have a character, for example: B. If it's possible to make a code, that would find all words, that starts with B character?
Asked
Active
Viewed 1.0k times
2
-
It is possible. So please try to write the code by yourself first. – Lee Duhem Mar 02 '14 at 10:48
-
possible duplicate of [Finding words with the same first character](http://stackoverflow.com/questions/22127149/finding-words-with-the-same-first-character) – Sergey Kalinichenko Mar 02 '14 at 12:07
3 Answers
1
you can divide the string using strtok() into words. once you get the words in first string, then you can check each word whether it starts with given letter. this link can give you idea about how to use strtok.
There are many ways to do this using the string library functions or by using string as character array. it is one of the direct methods that i told using strtok().
0
You can use strcmp() function. Its included under "string.h" header.
int strcmp (string 1, string2):This function compares two strings passed as parameters and returns either +ve number,0,-ve number.
+ve value indicates string1 > string2. 0 indicates string1 and string2 are equal -ve value indicates string1 < string2.

Prarthana Hegde
- 361
- 2
- 7
- 18
0
This may be helpful to you
#include<stdio.h>
#include<conio.h>
int main()
{
char *str;
char ch;
int i=0,j,count=0,len=0;
clrscr();
puts("Enter String");
gets(str);
puts("Enter Character");
scanf("%c",&ch);
len=strlen(str);
printf("All words that start with character %c\n\n",ch);
while(str[i]!='\0') //to traverse the string
{
if((i==0)&&str[i]==ch) //for first word
{
j=i;
while(str[j]!=' ')
{
printf("%c",str[j]);
j++;
}
printf(",");
}
if((str[i]==' ')&&(str[i+1]==ch))
{
j=i+1;
while(str[j]!=' '&&j<len) //for all other words
{ //j<len is used only if last word has same character
printf("%c",str[j]);
j++;
}
printf(",");
}
i++;
}
getch();
}

A.s. Bhullar
- 2,680
- 2
- 26
- 32