SOUNDEX PROJECT

write ac++ program which represents asoundex index system . your system acts as the following :
*reads string from afile "name.txt" .each string less than 16 characters long .this string represents sounding names or namees with similar spelling .
* your task is to convert asequense of names into the corresponding sonundex codees for easy retrieval .use afunction for conversion.
*stores the codes in an array and sorts is ascending.
*lets the main program write out both forms on afile "output.txt".
* asoundex code always consists of aletter followed by three digits . such that :
1/ the first letter of aname appears "unencoded"as the first character of the soundex code. and it's capitalized . it is also the only letter.
2/ the letters "a ;e;i;o;u;y;w;h" are never encoded when they are not the first character in aword . they do sreve .however ; to break sequences of like coded letters" see the next rule"
3/ all other letters encoded accoirding to the following table .excpet when they immediatly follow aletter "including the first letter" that would be encoded with the same code digit .according to the table :
1: B ,P,F,V.
2: C ,S,K,G,J,Q,X,Z.
3:D,T.
4:L.
5:M,N.
6:R.

EXAMPLE:
INPUT .... OUTPUT
LEE ..... LEE=>L000
Kuhne EBELL ................. Kuhne=>K500
EBELL=>E140
ebelson ebelson=>E142
SCHAEFER SCHAAK SCHAEFER=>S160
SCHAAK=>S200



PLEASE I WANR THE SOULUTION FOR THIS QUESTION BU USING CCTYPE AND OFSTREAM AND IOSTREAM.



THANK YOU
Last edited on
Please note, we are not here to do your homework. Start the code yourself, and if you have any issues related to your C++ code then feel free to post questions here for assistance.
hello every one
this is thse solve of problem.
this solve by Mr.mouhammad omar alqisi
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
 #include<iostream>
 #include<cctype>      //for toupper isdigit isalpha
 #include<fstream>     //to deal with files
 using namespace std;
 void code(char&);     // function prototype       
                 
 char arry[1000000][19];   
         
 void main()
 {
long r,row;    //r mean rowand row for row
 int countspace=0,output,  //output to output 0 after the conversion,countspace to neglegt the space 
	 c=0,col2,col;    //c & col & col2 for column 

 char cha,cha2,firstletter,//firstletter for the first letter of each name in the code
	 cha3,cha4;  //cha for reading,cha2for conversion,cha3 to specify the letter that will not encoded    
 long space=0;
 r=0;
 ifstream in;                //to read from the file      
 ofstream out;               //to write to another file
 in.open("names.txt");       //to open the input file
 out.open("oo.txt"); //to open the output file
 while(!in.eof())
 {
 space++; //to not make the first row in the array empty if the first char in the array is space 
	in.get(cha);
	if(cha==' '|| cha=='\n') 
	{
	
		if(countspace<1) {c=0; if(space!=1) r++; countspace++; }
		//to neglegt the space and to go to the next row in the arry after each name
	}
	else
	{
     if(isalpha(cha)) {arry[r][c]=cha; c++; countspace=0;}
	 //to save the name char char in the array
	}
 }
 
 for(row=0;row<=r;row++) //this loop for conversion and to call the conversion function
    {
		  
    firstletter=arry[row][0];
	firstletter=toupper(firstletter);
	arry[row][15]=firstletter; //to put the first letter in the cod e in its place [row][15]
    output=0;col2=16;           //col2 is the place of the first letter in the code;        
    for(col=1;col<15;col++) //we started from 1 (col=1)to except the first letter
	  {
      cha2=arry[row][col];
	  cha2=toupper(cha2);  //to make cha2 capital
	  cha4=cha2;           //we need cha4 in the condition* to know if the char is letter or not
	  cha3=arry[row][col-1];//to know if the letter in which immediatly follow is the same or not 
	  cha3=toupper(cha3);
      
	  if(col>0 && isalpha(cha4))//*
     	  {
          code(cha2);
		  code(cha3);
		  if(cha2!=cha3)               //to know if the tow letter has the same code or not
			  if(isdigit(cha2) ) {if(col2<19) {arry[row][col2]=cha2; col2++;}}   
		  //save the code in the array
	      }
	  }
    }

   for(row=0;row<=r;row++)    //this loop to print both the name and its code 
 {
	 output=0;
  for(col=0;col<19;col++)    //to print the name
  { 
	  cha=arry[row][col];
	  if(isalnum(cha)) out<<cha;
	  if(col==14) out<<" => ";    //to print => between the name and its code
	  if(col>15 && isdigit(cha))  //this condition to know how many digit is print
		  output++;
  }
  output=3-output;   //to print 0 after each code if the code is less than three digit 
  while(output>0) {out<<0;output--;} //example lee =>L000
 out<<endl;
 }

 }

   void code(char& cha)       //this function for conversions
   {
    if(cha=='P'||cha=='B'||cha=='F'||cha=='V') cha='1';
    else                                                  
    if(cha=='C'||cha=='S' ||cha=='Q' ||cha=='K' ||cha=='G'||cha=='J' ||cha=='X'||cha=='Z') 
    cha='2';
    else 
    if(cha=='M'||cha=='N' ) cha='5'; 
    else 
    if(cha=='D'||cha=='T' ) cha='3';
    else 
    if(cha=='L' ) cha='4';
    else 
    if(cha=='R' ) cha='6';
  }
Last edited on
Heh, well, since we're posting answers anyway, why not cheat with something that will get you expelled properly:

soundex.hpp
1
2
3
4
5
6
7
8
9
10
11
12
// soundex.hpp

#ifndef SOUNDEX_HPP
#define SOUNDEX_HPP

#include <string>

std::string soundex( const std::string& s );

#endif

// end soundex.hpp 


soundex.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// soundex.cpp
// Copyright (c) 2008 Michael Thomas Greer.
//
// Boost Software License - Version 1.0 - August 17th, 2003
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//

#include <algorithm>
#include <functional>
#include <string>
#include <cctype>

using namespace std;

//----------------------------------------------------------------------------
char f_transform( char c )
  {
  string consonants[ 6 ] = { "BFPV", "CGJKQSXZ", "DT", "L", "MN", "R" };
  for (int i = 0; i < 6; i++)
    if (consonants[ i ].find( c ) != string::npos)
      return (i +1) +'0';
  return c;
  }

//----------------------------------------------------------------------------
string soundex( const string& s )
  {
  string result;

  // Validate s
  if (std::find_if(
        s.begin(),
        s.end(),
        std::not1(std::ptr_fun<int,int>(std::isalpha))
        )
      != s.end())
    return result;

  // result <-- uppercase( s )
  result.resize( s.length() );
  std::transform(
    s.begin(),
    s.end(),
    result.begin(),
    std::ptr_fun<int,int>(std::toupper)
    );

  // Convert Soundex letters to codes
  std::transform(
    result.begin() +1,
    result.end(),
    result.begin() +1,
    f_transform
    );

  // Collapse adjacent identical digits
  result.erase(
    std::unique(
      result.begin() +1,
      result.end()
      ),
    result.end()
    );

  // Remove all non-digits following the first letter
  result.erase(
    std::remove_if(
      result.begin() +1,
      result.end(),
      std::not1(std::ptr_fun<int,int>(std::isdigit))
      ),
      result.end()
    );

  result += "000";
  result.resize( 4 );

  return result;
  }

// end soundex.cpp 


Soundex only works on the Roman alphabet, so I didn't bother to templatize it for Unicode.

[edit] Fixed a couple of typos.
Last edited on
Topic archived. No new replies allowed.