0

Let's say I input the letter:
c

it will show:
c - cat


How do I do that? Thank you!

4
Contributors
6
Replies
9
Views
5 Years
Discussion Span
Last Post by Adak
0

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.

0

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

Thanks.

Edited by Xinen: n/a

0

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 by Narue: n/a

0

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;
}
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 by zeroliken: n/a

0

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 by Adak: n/a

This article has been dead for over six months. 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.