7 Replies - 2525 Views - Last Post: 09 November 2008 - 12:32 PM Rate Topic: -----

#1 milos_r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-October 08

rearrange vowels then cons in given word, C program

Posted 09 November 2008 - 06:19 AM

So this is what I need to do. User input some word and program rearrange that so the vowels come first and cons second. I'm a beginner but i have some ideas. This is code so far:
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>

int main()
{
	char rec[20], vow[20], con[20], ch;
	int i;

	printf ("\n\n  enter word: ");
	gets (rec);
	for (i=0;i<strlen(rec);i++)
	{
	ch=rec[i];
	switch (ch)
		{
		case 'a':case 'e':case 'i':case 'o':case 'u':case 'A':case 'E':case 'I':case 'O':case 'U':vow[20]++;break;
		default: con[20]++;
		}

}
printf  ("\n  now its looks like this:%s  %s",vow,con);
}




I now this is wrong but I dont have other ideas, so any help will be appreciated .
Thanks in advance
Milos

Is This A Good Question/Topic? 0
  • +

Replies To: rearrange vowels then cons in given word, C program

#2 milos_r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-October 08

Re: rearrange vowels then cons in given word, C program

Posted 09 November 2008 - 10:22 AM

Here is new version, now its working but its not yet over
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>

int main()
{
	char rec[20],rec1[20], rec2[20], ch;
	int i,j=0,k=0;

	printf ("\n\n  enter word: ");
	gets (rec);
	for (i=0;i<strlen(rec);i++)
	{
	ch=rec[i];
	switch (ch)
		{
		case 'a':case 'e':case 'i':case 'o':case 'u':case 'A':case 'E':case 'I':case 'O':case 'U':rec1[j]=ch;j++;break;
		default: rec2[k]=ch;k++;
		}

}
printf  ("\n  now it look like this:%s%s",rec1,rec2);
}




So if I input for example dreamincode output will be "eaioedrmncd├w.├wΦ→╞w\". Why is this happening???
Was This Post Helpful? 0
  • +
  • -

#3 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: rearrange vowels then cons in given word, C program

Posted 09 November 2008 - 10:58 AM

It's because the parts of the array that you don't use is still memory that is filled with characters. You reserve memory for 20 characters but you only assign part of it, the rest contains old useless information.

You can either solve this by printing only the part of the array that corresponds to the length of the initial word, or you can add the character '\0' (end of string character) to your string when you're done putting the characters in order.

This post has been edited by Gloin: 09 November 2008 - 10:59 AM

Was This Post Helpful? 0
  • +
  • -

#4 milos_r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-October 08

Re: rearrange vowels then cons in given word, C program

Posted 09 November 2008 - 11:24 AM

View PostGloin, on 9 Nov, 2008 - 09:58 AM, said:

It's because the parts of the array that you don't use is still memory that is filled with characters. You reserve memory for 20 characters but you only assign part of it, the rest contains old useless information.

You can either solve this by printing only the part of the array that corresponds to the length of the initial word, or you can add the character '\0' (end of string character) to your string when you're done putting the characters in order.


Can you please put that solution in code.
I was trying with this but it dosent work
printf  ("\n  nova rec izgleda ovako:%s%s",rec1[j],rec2[k])


Thanks in advance
Milos
Was This Post Helpful? 0
  • +
  • -

#5 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: rearrange vowels then cons in given word, C program

Posted 09 November 2008 - 11:53 AM

Is the goal to rearrange letters in each word, or for the entire sentence?
e.g
input: hello world
output: oehll owrld
OR
output: eoohllwrld

if the goal is to rearrange the entire sentence then I would probably have one array for input and one for the rearranged letters. I would use two for loops the first one would check for vowels and the second for consonants. Like so:
for (i = 0; i < strlen(input); i++)
{
	char ch = tolower(input[i] );
	if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
		output[count++] = input[i];
}

for (i = 0; i < strlen(input); i++)
{
	char ch = tolower(input[i] );
	if (ch > 'a' && ch <= 'z' && ch != 'e' && ch != 'i' ch != 'o' && ch != 'u')
		output[count++] = input[i];
}

output[counter] = '\0';
printf("%s\n", output);


This post has been edited by GWatt: 09 November 2008 - 11:54 AM

Was This Post Helpful? 0
  • +
  • -

#6 milos_r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-October 08

Re: rearrange vowels then cons in given word, C program

Posted 09 November 2008 - 12:05 PM

View PostGWatt, on 9 Nov, 2008 - 10:53 AM, said:

Is the goal to rearrange letters in each word, or for the entire sentence?
e.g
input: hello world
output: oehll owrld
OR
output: eoohllwrld

if the goal is to rearrange the entire sentence then I would probably have one array for input and one for the rearranged letters. I would use two for loops the first one would check for vowels and the second for consonants. Like so:
for (i = 0; i < strlen(input); i++)
{
	char ch = tolower(input[i] );
	if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
		output[count++] = input[i];
}

for (i = 0; i < strlen(input); i++)
{
	char ch = tolower(input[i] );
	if (ch > 'a' && ch <= 'z' && ch != 'e' && ch != 'i' ch != 'o' && ch != 'u')
		output[count++] = input[i];
}

output[counter] = '\0';
printf("%s\n", output);



The goal is to rearrange letters in only one word. So i can use scanf function instead of gets.
e.g
input: dreamincode
output: eaioedrmncd
my current output:eaioedrmncd├w.├wΦ→╞w\

No need for whole sentence.
Actually the code in my last post work fine i only need to figure it out where to put that '\0' end of string, or how to print only used part of arays.
Thanks
Milos
Was This Post Helpful? 0
  • +
  • -

#7 GWatt  Icon User is offline

  • member icon

Reputation: 264
  • View blog
  • Posts: 3,059
  • Joined: 01-December 05

Re: rearrange vowels then cons in given word, C program

Posted 09 November 2008 - 12:09 PM

rec1[j] = '\0';
rec2[k] = '\0';


after your for loop finishes
Was This Post Helpful? 0
  • +
  • -

#8 milos_r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-October 08

Re: rearrange vowels then cons in given word, C program

Posted 09 November 2008 - 12:32 PM

View PostGWatt, on 9 Nov, 2008 - 11:09 AM, said:

rec1[j] = '\0';
rec2[k] = '\0';


after your for loop finishes


Thanks GWatt,
Now its working fine. Finally.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1