Let's say I input the letter:
c

it will show:
c - cat


How do I do that? Thank you!

How do I do that?

Well, you need some sort of dictionary to look up the words, then pick a word that starts with the letter when there are more than one.

Yes, but what shall I use, an if-else statement? Array?

Thanks.

Edited 4 Years Ago by Xinen: n/a

Use whatever you want unless your homework (and this is clearly homework) requires a specific control structure. There's not a single way to accomplish most programming problems, so you need to figure out which ones will work and choose between them.

Edited 4 Years Ago by Narue: n/a

This is what I have done: if-else, although in c++, can anyone convert to c?

#include <iostream>
#include <string>
using namespace std;
int main ()
{
using std::string;
string letter;

cout << "Enter a letter: ";
cin >> letter;

if (letter == "a")
{
cout << "aqua" << endl;
}
		
if (letter == "b")
{
cout << "blue" << endl;
}

return 0;
}
#include <iostream> //#include <stdio.h>
#include <string>
using namespace std;
int main (void)
{
using std::string;
string letter;

cout << "Enter a letter: "; //use printf() instead of cout
cin >> letter;              //use scanf() instead of cin

if (letter == "a")
{
cout << "aqua" << endl;
}
		
if (letter == "b")
{
cout << "blue" << endl;
}

return 0;
}

It's your job to convert your program to c
search the web on examples of c programs

Edited 4 Years Ago by zeroliken: n/a

As a beginner in C, you DEFINITELY want to use an array of chars for the words:
words[NumberOfWords][40].

Where NumberOfWords could be just a few hundred or 40,000 or more. There are very few English words that are longer than 39 letters, (save 1 space for the end of string char), outside of scientific nomenclature.

You'll want to declare this array above main(), to get global memory (heap), not try to use the smaller stack inside a function, for it.

Word lists are available on the net, btw. A single word can then be referenced with
words, in a loop, like:


int i=0;
while((fscanf(YourFilePointer, "%s", words[i]))>0)
   ++i;

Edited 4 Years Ago by Adak: n/a

This article has been dead for over six months. Start a new discussion instead.