Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While I waited for you to post this as a question I coded something up.</p> <p>This code iterates through a string passed to a "longest" function, and when it finds the first of a sequence of letters it sets a pointer to it and starts counting the length of it. If it is the longest sequence of letters yet seen, it sets another pointer (the '<code>maxStringStart</code>' pointer) to the beginning of that sequence until it finds a longer one.</p> <p>At the end, it allocates enough room for the new string and returns a pointer to it.</p> <pre><code>#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; #include&lt;string.h&gt; int isLetter(char c){ return ( (c &gt;= 'a' &amp;&amp; c &lt;= 'z') || (c &gt;= 'A' &amp;&amp; c &lt;= 'Z') ); } char *longest(char *s) { char *newString = 0; int maxLength = 0; char *maxStringStart = 0; int curLength = 0; char *curStringStart = 0; do { //reset the current string length and skip this //iteration if it's not a letter if( ! isLetter(*s)) { curLength = 0; continue; } //increase the current sequence length. If the length before //incrementing is zero, then it's the first letter of the sequence: //set the pointer to the beginning of the sequence of letters if(curLength++ == 0) curStringStart = s; //if this is the longest sequence so far, set the //maxStringStart pointer to the beginning of it //and start increasing the max length. if(curLength &gt; maxLength) { maxStringStart = curStringStart; maxLength++; } } while(*s++); //return null pointer if there were no letters in the string, //or if we can't allocate any memory. if(maxLength == 0) return NULL; if( ! (newString = malloc(maxLength + 1)) ) return NULL; //copy the longest string into our newly allocated block of //memory (see my update for the strlen() only requirement) //and null-terminate the string by putting 0 at the end of it. memcpy(newString, maxStringStart, maxLength); newString[maxLength + 1] = 0; return newString; } int main(int argc, char *argv[]) { int i; for(i = 1; i &lt; argc; i++) { printf("longest all-letter string in argument %d:\n", i); printf(" argument: \"%s\"\n", argv[i]); printf(" longest: \"%s\"\n\n", longest(argv[i])); } return 0; } </code></pre> <p>This is my solution in simple C, without any data structures.</p> <p>I can run it in my terminal like this:</p> <pre><code>~/c/t $ ./longest "hello there, My name is Carson Myers." "abc123defg4567hijklmnop890" longest all-letter string in argument 1: argument: "hello there, My name is Carson Myers." longest: "Carson" longest all-letter string in argument 2: argument: "abc123defg4567hijklmnop890" longest: "hijklmnop" ~/c/t $ </code></pre> <p>the criteria for what constitutes a letter could be changed in the <code>isLetter()</code> function easily. For example:</p> <pre><code>return ( (c &gt;= 'a' &amp;&amp; c &lt;= 'z') || (c &gt;= 'A' &amp;&amp; c &lt;= 'Z') || (c == '.') || (c == ' ') || (c == ',') ); </code></pre> <p>would count periods, commas and spaces as 'letters' also.</p> <hr> <p>as per your update:</p> <p>replace <code>memcpy(newString, maxStringStart, maxLength);</code> with:</p> <pre><code>int i; for(i = 0; i &lt; maxLength; i++) newString[i] = maxStringStart[i]; </code></pre> <p>however, this problem would be much more easily solved with the use of the C standard library:</p> <pre><code>char *longest(char *s) { int longest = 0; int curLength = 0; char *curString = 0; char *longestString = 0; char *tokens = " ,.!?'\"()@$%\r\n;:+-*/\\"; curString = strtok(s, tokens); do { curLength = strlen(curString); if( curLength &gt; longest ) { longest = curLength; longestString = curString; } } while( curString = strtok(NULL, tokens) ); char *newString = 0; if( longest == 0 ) return NULL; if( ! (newString = malloc(longest + 1)) ) return NULL; strcpy(newString, longestString); return newString; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload