Thread: Here I go again...arrays....char...

  1. #1
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78

    Here I go again...arrays....char...

    My code is here! Vowels from sentence in one array, all none vowels in other array. First one is working but I can't get it to put all others in other array! Help!

    Code:
    int main()
    
    {
    	int sranje;
    	char niz[100];
    	char niz2[100]={NULL};
    	char niz4[100]={NULL};
    	char niz3[]={'a','A','e','E','U','u','i','I','o','O'};
    	int a,b,c=0,d=0,br=0;
    
    	printf("Upisi niz: ");
    	fgets(niz, sizeof(niz), stdin);
    
    for(a=0;a<strlen(niz)+1;a++)
    {
    		if(niz[a]==' ')
    			br++;
    	for(b=0;b<strlen(niz3)+1;b++)
    	{
    		if(niz[a]==niz3[b])
    		{
    			niz2[c]=niz[a];
    			c++;
    		}
    	}
    }
    		puts(niz2);
    		printf("\n");
    		puts(niz4);
    		printf("\n%d", br);
    
    	scanf("%d", &sranje);
    }
    I tried with combinations like this,
    Code:
                               f(niz[a]!=niz3[b])
    			{
    				niz4[d]=niz[a];
    				d++;
    			}
    but, nothing.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    strlen(niz3)

    to use this your niz3 should be null-terminated.
    In your case - it is not. It is just a char array, so no string manipulation routines can be applied.
    Or use sizeof(niz3)
    Or initialize it in another way - to get the null-termination character. For example
    Code:
    char niz3[]="aAeEUuiIoO";
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    I did and if I implement this

    Code:
    if(niz[a]!=niz3[b])
    			{
    				niz4[d]=niz[a];
    				d++;
    			}
    after first if, and input is "aaa", then it returns for "niz4" - aaaaaaaaaaaaaaaaaaaa

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Adding one to the results of strlen is basically saying "explicit buffer overrun here please". Then leaving off the nul terminator char, well you just really really want to crash something don't you?

    I advise single-stepping the whole way through your program.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Well the a proper solution is to test every letter of the input buffer to see whether is a vowel or not. Each time add every letter to its category and get the final results.
    I think that a simple answer code is the following, think of bugs included.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    const char *Vowls = "aeiou";
    
    int GetVowels(const char *Buf, char **Vowels, char **NoVowels)
    {
    	if(Buf == NULL)
    	{
    		printf("Empty Buffer.\n");
    		return -1;
    	}
    	else
    	{
    		//Variables.
    		int i;
    		int j = 0;
    		int n = 0;
    		//Get memory for the pointers.
    		*Vowels = calloc(1, sizeof(char));
    		*NoVowels = calloc(1, sizeof(char));
    		//Check for memory fault.
    		if(*Vowels == NULL || *NoVowels == NULL)
    		{
    			printf("Memory Error.\n");
    			return -2;
    		}
    		else
    		{
    			//Search each letter seperately to find what you need, save it to char arrays.
    			for(i = 0; i < (int)strlen(Buf); i++)
    			{
    				//Check for valid input, you can do whatever you want with it, i prefer to break as an invalid input.
    				if(Buf[i] == ';' || Buf[i] == ',' || Buf[i] == '!' || Buf[i] == ':')
    					break;
    				if(strchr(Vowls, Buf[i]) != NULL)
    				{
    					Vowels[0] = realloc(Vowels[0], sizeof(char) * (j+1));
    					*(Vowels[0]+ j) = Buf[i];
    					j++;
    				}
    				else
    				{
    					NoVowels[0] = realloc(NoVowels[0], sizeof(char) * (n+1));
    					*(NoVowels[0]+ n) = Buf[i];
    					n++;
    				}
    			}
    			if(i == strlen(Buf))
    			{
    				//Get space for the '\0'.
    				Vowels[0] = realloc(Vowels[0], sizeof(char) * (j+1));
    				NoVowels[0] = realloc(NoVowels[0], sizeof(char) * (n+1));
    				//Put it.
    				*(Vowels[0]+ j) = '\0';
    				*(NoVowels[0]+ n) = '\0';
    				return 0;
    			}
    			else
    			{
    				printf("Internal Loop Error, wrong misplaced symbol &#37;c in Buffer in place %d.\n", Buf[i], i);
    				free(*Vowels);
    				free(*NoVowels);
    				*Vowels = NULL;
    				*NoVowels = NULL;
    				return -3;
    			}
    		}
    	}
    }
    int main(int argc, char *argv[])
    {
    	char Buf[256] = "";
    	char *nv = NULL;
    	char *v = NULL;
    	printf("Give me the sentance:");
    	fgets(Buf, sizeof(Buf), stdin);
    	if(Buf[strlen(Buf) - 1] == '\n')
    		Buf[strlen(Buf) - 1] = '\0';
    	GetVowels(Buf, &v, &nv);
    	printf("NoVowels: %s\n",nv);
    	printf("Vowels:   %s\n",v);
    	free(v);
    	free(nv);
    	return 0;
    }
    I dont remember exactly if the Vowels are these i declared but you can easily add or remove letters from the string before the function!!!

    Some results:

    Give me the sentance:Hi this is the new way of making code less friendly
    NoVowels: H ths s th nw w f mkng cd lss frndl
    Vowels: iiieeayoaioeeiey
    Press any key to continue . . .

    Bokarinho!
    Last edited by Bokarinho; 09-10-2007 at 02:37 AM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Bokarinho, that's an awful lot of energy to expend on a simple sieve. You sure like to be verbose! There exists a function in the standard C string library that can make such things a trivial exercise.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by citizen View Post
    Bokarinho, that's an awful lot of energy to expend on a simple sieve. You sure like to be verbose! There exists a function in the standard C string library that can make such things a trivial exercise.
    Seconded. That's a wicked amount of wheel reinvention there. That and 'y' is not a vowel.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Quote Originally Posted by iMalc View Post
    Seconded. That's a wicked amount of wheel reinvention there. That and 'y' is not a vowel.
    Thanks for your great commentary.... Yes y is not a vowel you can easily remove it from the string and its all ok!!! Whats the problem with you people??????

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Programmers are pedantic by nature

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  10. #10
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    Bokarinho
    I think that a simple answer code is the following, think of bugs included.
    thx, but i think there is a much simpler way to do this, but i can learn something from your code, if i can't use that in this task ill use it in another.

  11. #11
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    Adding one to the results of strlen is basically saying "explicit buffer overrun here please". Then leaving off the nul terminator char, well you just really really want to crash something don't you?
    can you please be more specific. My new code looks like this now:
    Code:
    	char niz[100];
    	char niz2[100]={NULL};
    	char niz4[100]={NULL};
    	char niz3[]="aAeEiIoOuU";
    	int a,b,c=0,d=0,br=0;
    	int i, j, temp=0;
    
    	printf("Upisi niz: ");
    	fgets(niz, sizeof(niz), stdin);
    
    for(a=0;a<strlen(niz);a++)
    {
    		if(niz[a]==' ')
    		{
    			br++;
    		}
    	for(b=0;b<strlen(niz3);b++)
    	{
    		if(niz[a]==niz3[b])
    		{
    			niz2[c]=niz[a];
    			c++;
    		}
    		if(niz[a]!=niz3[b])
    		{
    			niz4[d]=niz[a];
    			d++;
    		}
    		 
    	}
    }
    like i said before, vowels ok aa , novowels puts : aaaaaaaaaaaaaaaaaaaaaaaa

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > like i said before, vowels ok aa , novowels puts : aaaaaaaaaaaaaaaaaaaaaaaa

    You need to work ouy a way to check all of the vowels at once, given some letter in an input string. Right now, you're code only looks for 'a'. What you have simply doesn't work for the same reason that 'a' is not 'e' or any other letter.

    Look up a function called strchr(), find out what it does, and think about how it can help you with your new problem. Or strcspn(), again.

  13. #13
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    Well, fu**'it, I can always do it like this, nice and sorted!!!

    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    	int sranje;
    	char niz[100];
    	char niz2[100]={NULL};
    	char niz4[100]={NULL};
    	char niz3[]="aAeEiIoOuU";
    	char niz5[]="bBcCdDfFgGhHkKlLpPwWrRtTzZQqsSyYxXjJVMmnN";
    	int a,b,c=0,d=0,br=0;
    	int i, j, temp=0;
    
    	printf("Upisi niz: ");
    	fgets(niz, sizeof(niz), stdin);
    
    for(a=0;a<strlen(niz)-1;a++)
    {
    		if(niz[a]==' ')
    		{
    			br++;
    		}
    	for(b=0;b<strlen(niz3)-1;b++)
    	{
    		if(niz[a]==niz3[b])
    		{
    			niz2[c]=niz[a];
    			c++;
    		}
    	}
    	for(b=0;b<strlen(niz5)-1;b++)
    	{
    		if(niz[a]==niz5[b])
    		{
    			niz4[d]=niz[a];
    			d++;
    		}
    	}
    }
    for(c=0;c<strlen(niz2);c++)
    {
    	for(j=0;j<strlen(niz2);j++)
    	{
    		if(niz2[c]<niz2[j])
    		{
    			temp=niz2[c];
    			niz2[c]=niz2[j];
    			niz2[j]=temp;
    		}
    	}
    }
    for(c=0;c<strlen(niz4);c++)
    {
    	for(j=0;j<strlen(niz4);j++)
    	{
    		if(niz4[c]<niz4[j])
    		{
    			temp=niz4[c];
    			niz4[c]=niz4[j];
    			niz4[j]=temp;
    		}
    	}
    }
    		puts(niz2);
    		printf("\n");
    		puts(niz4);
    		printf("\n&#37;d", br);
    
    	scanf("%d", &sranje);
    }
    Last edited by shardin; 09-10-2007 at 04:14 AM.

  14. #14
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You forgot jJ in your consonant string...
    and you've got cC in there twice...

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  15. #15
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    it's there now...
    ...and aprentice shall become master...or not...

    "Never let your sense of moral prevent you from doing what is right!" Salvor Hardin, mayor of Terminus

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  2. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM