GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Feb-2008, 13:32
ashayah ashayah is offline
New Member
 
Join Date: Oct 2007
Posts: 12
ashayah is an unknown quantity at this point
Post

Using String function in C language



..this program is about FLAMES.. the so called "game"..and i think many of you are familiar about this one..am i right?..

this code only prints the total letter of the two names entered and its corresponding letter after summing it up..instead of the the canceled letters(the same letters that the two names have/ common letters..as what the FLAMES rule is..canceling and summing the canceled letters..)

my code problem is, i don't know how to count the canceled letters, yet i know how to sum it up and its corresponding letter in FLAMES. for example.. 1st name: Marie Yu 2nd name: Mark Cruz.. 2 m's will be canceled, 2 a's, 3 r's, and 2 u's..sO, the total letters that were canceled were 9..so, 9 is "A" which means "ADMIRED"..1-FRIENDS, 2-LOVE, so on...

my code is: im using C LANG. compiler: BORLAND C++.,

CPP / C++ / C Code:
#include<stdio.h>
#include<string.h>

void main()
{
	 char str1[20];
	 char str2[20];
	 int m;

	 printf("Enter a name: \n");
	 gets(str1);


	 printf("Enter another name: \n");
	 gets(str2);

	 if(strcmp(str1,str2) != 0) {
		if(strcat(str1 ,str2)

		  m = (int)(strlen(str1) + strlen(str2)) % 6;


		  switch(m){
				case 0: printf("Friends")
				case 1: printf("Lovers")
				case 2: printf("Anger")
				case 3: printf("Marriage")
				case 4: printf("Enemies")
				case 5: printf("Sweethearts"} 		  }
	 }
}

thanks ahead.!=)


  #2  
Old 03-Feb-2008, 13:03
aijazbaig1's Avatar
aijazbaig1 aijazbaig1 is offline
Member
 
Join Date: May 2006
Location: India
Posts: 174
aijazbaig1 has a spectacular aura aboutaijazbaig1 has a spectacular aura about

Re: using String function in C language


welll..
I would suggest dividing the problem into functions. One of the functions would divide a given strings into characters and assign weights to each such unique character which would indicate the number of times it has occured in a given string. This could be done by creating an array of structs dynamically (see example below).

A second function would then take (pointers to)two such newly created arrays and in that function I would:

1. determine which of the two strings is shorter in size.
2. then take the shorter string as the base string and then do a character by character comparison of each of the letters of the shorter string with each letter of the larger string.
3. When a match is found then I would add the weights of the characters which have been found to match.

Your implementation may vary. I may convert the arrays into arrays of structs with the struct having two members, one being the character and the other being the weight of the character.

Comparison would be a bit more difficult. You may have to compare the character part of the structure until you reach the 'null' character in the shorter array of structs.

Hence 'Marie Moon' would transform into {(2,m),(1,a),(1,r),(1,i),(1,e),(2,o),(1,n),'\0'}
and Mark Cruz would become {(1,m),(1,a),(2,r),(1,k),(1,c),(1,u),(1,z),'\0'}

In the case above both the structs are equal in length. You may come across long strings with a high rate of repetition of chars being compared with relatively shorter strings with very little or no repetition. In cases like that the transformation would leave the former string shorter than the latter in the array of structs.

Anyways, I guess I have given you something to work with...I hope it helps..and you can use isxxx types of functions from ctype.h to ignore white space incase the string contains two or more words.
__________________
Hope to hear from you guys!

--------------------------------------------------

Best Regards,
Aijaz Baig.
  #3  
Old 04-Feb-2008, 02:54
aijazbaig1's Avatar
aijazbaig1 aijazbaig1 is offline
Member
 
Join Date: May 2006
Location: India
Posts: 174
aijazbaig1 has a spectacular aura aboutaijazbaig1 has a spectacular aura about

Re: using String function in C language


Hi there,
sorry I guess I didn't read your question thoroughly. I think you wanted to know how to count the cancelled letters.

Well..what I might do is I might first concatenate both the strings and then remove all the white space that occurs.

Then I would apply any sorting algorithm on that string which would then sort the characters (from a to z). Hence we would be left with a sorted strings which may have substrings composed of the same character.

I would have to just scan the string of chars checking for repetition of chars and have a counter that would increment whenever I come across a repetition. Note that since we are only concerned with the total number of letters that cancel or the total 'number of repetitions' we do not need to remember the characters which cancel. One dummy char variable should do the job.
__________________
Hope to hear from you guys!

--------------------------------------------------

Best Regards,
Aijaz Baig.
  #4  
Old 05-Feb-2008, 13:15
ashayah ashayah is offline
New Member
 
Join Date: Oct 2007
Posts: 12
ashayah is an unknown quantity at this point
Question

Re: Using String function in C language


Thanks guys! I understand what you were telling but can you please give me a code that can help me further? In accordance to what I posted? About FLAMES!
  #5  
Old 05-Feb-2008, 13:44
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 969
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Using String function in C language


Is this what you're looking for?

CPP / C++ / C Code:
// Counts the number of canceled letters and returns it
int NumberOfCanceledLetters( char *str1, char *str2 )
{ int Count[26]; // one for each letter in the alphabet
  int i,len;
  char c;
  int result;

  // Initialize Count
  for(i=0;i<26;++i) Count[i] = 0;

  // Process str1
  len = strlen(str1);
  for(i=0;i<len;++i)
  { c = toupper( str1[i] ); 
    if(isalpha(c)) Count[c-'A']++;
  }

  // Process str2
  len = strlen(str2);
  for(i=0;i<len;++i)
  { c = toupper( str2[i] ); 
    if(isalpha(c)) Count[c-'A']++;
  }
  
  // Tally the results
  for(i=0,result=0;i<26;++i) 
  { if( Count[i] > 1 ) result += Count[i];
  }
  
  return result;
}
  #6  
Old 06-Feb-2008, 07:40
ashayah ashayah is offline
New Member
 
Join Date: Oct 2007
Posts: 12
ashayah is an unknown quantity at this point

Re: Using String function in C language


actually, i dont know where should i put what you have added..where should i put that one??
  #7  
Old 06-Feb-2008, 08:21
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 969
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Using String function in C language


Try putting it here:
Quote:
Originally Posted by ashayah
CPP / C++ / C Code:
	 if(strcmp(str1,str2) != 0) {
		//if(strcat(str1 ,str2)

		//m = (int)(strlen(str1) + strlen(str2)) % 6;
		m = NumberOfCanceledLetters( str1, str2 );


		  switch(m){
				case 0: printf("Friends")
				case 1: printf("Lovers")
				case 2: printf("Anger")
				case 3: printf("Marriage")
				case 4: printf("Enemies")
				case 5: printf("Sweethearts"} 		  }
  #8  
Old 06-Feb-2008, 08:57
aijazbaig1's Avatar
aijazbaig1 aijazbaig1 is offline
Member
 
Join Date: May 2006
Location: India
Posts: 174
aijazbaig1 has a spectacular aura aboutaijazbaig1 has a spectacular aura about

Re: Using String function in C language


hmmm..pretty compact code fakepoo!!!..never occured to me it could be this easy...well...you thought in terms of the problem at hand and I wandered in terms of representation...making ur suggestion a much more natural solution. Do you know where I could learn some methodologies of creating algorithms like this. Like programming methodologies for instance.

Hope to hear from you
__________________
Hope to hear from you guys!

--------------------------------------------------

Best Regards,
Aijaz Baig.
  #9  
Old 06-Feb-2008, 09:25
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 969
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Using String function in C language


Well, I don't exactly know where a good site is for creating algorithms with methodologies but maybe some of the others in this forum would know.

Basically, if there is a section of code that performs a specific task, I like to put it into it's own function to make the code cleaner. This makes it easier to debug, reuse, and read to get an understanding of what is happening. In order to create a function, you need to figure out exactly what the function needs to do, what it needs to return as output, and what it needs as input in order to perform it's task.

Also, commenting in your code is a good habit to get into so that you can look back at the code at a later time and realize what is happening without having to dissect it again.

For the most part, continue practicing and you'll get better as you go. A forum like this one is very helpful for getting advice about your code and ways you can improve it.
  #10  
Old 07-Feb-2008, 05:35
davis
 
Posts: n/a

Re: Using String function in C language


CPP / C++ / C Code:
int NumberOfCanceledLetters( const char *str1, const char *str2 )

fakepoo, don't forget to use proper const correctness!


:davis:
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Flex and bison coding lucky88star C++ Forum 5 24-Dec-2007 11:57
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 16:19
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
Help wit my source code compiler errors Krandygrl00 C++ Forum 1 06-Jun-2005 08:14
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:33.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.