find the smallest word in the string. revise.

hi everyone, my old thread became old, but im here with another plea. i have to re-do the program without utilizing any external libraries in the code.
i have to rewrite it in plain c..i can use ctype as a headerfile..

here's what i have right now (thanks to vlad from moscow):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <vector>
#include <conio.h>
#include <iterator>
#include <sstream>
#include "stdafx.h"
 
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;
 
 

using namespace std;


int main()
{


	std::string s;


	std::cout<<"Please enter your sentence"<<std::endl;

	std::getline(cin, s);

	std::istringstream is( s );

	
	
	std::string minimum;
	bool first = true;

	for ( auto it = std::istream_iterator<std::string>( is );
		  it != std::istream_iterator<std::string>();
		  ++it )
	{
		if ( first ) first = false, minimum = *it;
		else if ( ( *it ).size() < minimum.size() ) minimum = *it;
	}

	std::cout << "Minimum = \"" << minimum << "\"" << std::endl;

	getch();
	return 0;
}



i will thank like crazy for anyone that helps...:)
Do you mean C? Not C++?
yes. without the use of libraries like vector for instance...i can use ctype...im sorry for constantly asking for help, but its just that no one is...
Last edited on
In this case you shall also use <stdio.h> for i/o operations.

The start sample will look like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <ctype.h>
#include <conio.h>

#define SkipSpaces( p ) while( isspace( *( p ) ) ) ++( p )
#define ExtractWord( p ) while ( !isspace( *( p ) ) && *( p ) ) ++( p )

int main( void )
{
   char s[80];
   const char *p = s;

   puts( "Press any key to continue..." );
   getch();
   return ( 0 );
}


Try to use these macros to extract words from the string.
this is what i did:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include "stdafx.h"

#define SkipSpaces( p ) while( isspace( *( p ) ) ) ++( p )
#define ExtractWord( p ) while ( !isspace( *( p ) ) && *( p ) ) ++( p )

int main( void )
{
   char s[80];
   char *p = s;
   
   
   puts("Please enter your sentence");



   for(int i = 0 ; i < 80; i++){
   scanf("%c", s[i]);
}
   for(;p<s+80;++p){

	  s[*p];

	 SkipSpaces(p);
	 ExtractWord(p);

   }


    for(int i = 0 ; i < 80; i++){

   printf("%c", s[i]);

}


   
   

   puts( "Press any key to continue..." );
   getch();
   return ( 0 );
}



code returns an error.... :/ can anyone assist me with this
Last edited on
Look standard function fgets to read a whole string until ENTER will be pressed.

char * fgets( char *s, int n, FILE *stream )

I think it is the function you shall use to read a sentence.

For example


1
2
3
4
...
   puts( "Please enter your sentence" );
   fgets( s, sizeof( s ), stdin );
... 
Last edited on
it continues returning an error...
Last edited on
Show your codde and error messages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include "stdafx.h"

#define SkipSpaces( p ) while( isspace( *( p ) ) ) ++( p )
#define ExtractWord( p ) while ( !isspace( *( p ) ) && *( p ) ) ++( p )

int main( void )
{
   char s[80];
   char *p = s;
   
   
   puts( "Please enter your sentence" );
   fgets( s, sizeof( s ), stdin );


   for(;p<s+80;++p){

	  s[*p];

	 SkipSpaces(p);
	 ExtractWord(p);

   }


    for(int i = 0 ; i < 80; i++){

   printf("%c", s[i]);

}


   
   

   puts( "Press any key to continue..." );
   getch();
   return ( 0 );
}




Unhandled exception at 0x5894de8f (msvcr100d.dll) in Job9.exe: 0xC0000005: Access violation writing location 0xffffffcc.
Last edited on
Does the following code work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include "stdafx.h"

#define SkipSpaces( p ) while( isspace( *( p ) ) ) ++( p )
#define ExtractWord( p ) while ( !isspace( *( p ) ) && *( p ) ) ++( p )

int main( void )
{
   char s[80];
   char *p = s;
   char *q;
   char *min_pos = NULL;
   int min_len = 0;
   
   puts( "Please enter your sentence" );
   fgets( s, sizeof( s ), stdin );

   while ( *p )
   {
      SkipSpaces( p );
      if ( *p )
      {
         q = p;
         ExtractWord( p );
         if ( min_pos == NULL || p - q < min_len )
         {
            min_pos = q;
            min_len = p - q;
         }
      }
   }
     
   if ( min_pos != NULL )
   {
      *( min_pos + min_len ) = '\0';
      printf( "Minimum word is \"%s\"\n", min_pos );
   }         

   puts( "Press any key to continue..." );
   getch();
   return ( 0 );
}
Last edited on
Yes it does! Thank you!!

Can I ask, is this correct??


1
2
3
4
5
6
7
8
 for(;p<s+80;++p){

	  s[*p];

	 SkipSpaces(p);
	 ExtractWord(p);

   }



can you generally tell me whats wrong with my code??
What is it ?

s[*p];

*p is some symbol in your string. You use that symbol as an index in your array. For example if *p is equal to 'a' which has code 97 then you reference to memory outside your array.


Last edited on
Topic archived. No new replies allowed.