Let's say I input the letter:
c

it will show:
c - cat


How do I do that? Thank you!

Recommended Answers

All 6 Replies

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.

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.

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

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