22271

Finding words with the same first character

Question:

I've made an array and now I'm trying to compare first symbols of two strings and if it's true to print that word. But I got a problem:

<blockquote>

Incompatible types in assignmentof "int" to "char"[20]"

</blockquote>

Here is the code:

for ( wordmas= 0; i < character; i++ ) { do { if (!strncmp(wordmas[i], character, 1) } puts (wordmas[i]); }

Maybe you guys could help me?

Answer1:

There are several issues with your code:

<ul><li>You do not need strncmp to compare the first character - all you need is a simple == or !=.</li> <li>Using a do without a while is a syntax error; you do not need a nested loop to solve your problem.</li> <li>character is used to limit the progress of i in the outer loop, and also to compare to the first character of a word in wordmas[i]. This is very likely a mistake.</li> <li>Assuming that wordmas is an array, assigning to wordmas in the loop header is wrong.</li> </ul>

The code to look for words that start in a specific character should look like this:

char wordmas[20][20]; ... // read 20 words into wordmas char ch = 'a'; // Look for all words that start in 'a' // Go through the 20 words in an array for (int i = 0 ; i != 20 ; i++) { // Compare the first characters if (wordmas[i][0] == ch) { ... // The word wordmas[i] starts in 'a' } }

Recommend

  • Some help writing a s-function to read data from serial port
  • Word search algorithm using an m.file
  • nasm issue relocation R_X86_64_PC32 shared library
  • XMPP push notifications causing problems (delay + duplications) in messages
  • How do I stop js files being cached in IE?
  • Erlang raise number of opened sockets, MacOS and CentOs
  • Using C# Attributes and documentation
  • JAR file: Could not find main class
  • Qt Connect signals with different arguments
  • Why are my web pages zoomed in when I open them in Opera Mobile?
  • How can I encode a filename according to RFC 2231?
  • Using HTML/CSS for UI in XNA?
  • C function strchr - How to calculate the position of the character?
  • hibernate sets dirty flag (and issues update) even though client did not change value
  • SonarQube: Cannot deactivate rule with missing quality profile
  • Trying to get the char code of ENTER key
  • android google indoor map
  • How to generate and display a QR Code in ionic 2
  • Unable to install Git-core+svn by MacPorts
  • Django simple Captcha “No module named fields” error
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Could not find rake using whenever rails
  • Atlas images wrong size on iPad iOS 9
  • AES padding and writing the ciphertext to a disk file
  • Convert array of 8 bytes to signed long in C++
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Linker errors when using intrinsic function via function pointer
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • LevelDB C iterator
  • How can I remove ASP.NET Designer.cs files?
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal