Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

c vowel task

Options
  • 13-01-2008 7:03pm
    #1
    Registered Users Posts: 23


    Hi,
    I'm quite new to programming so please excuse the fact that this is probably an easy problem. i'm trying to solve the following problem:
    Description
    Read in a line of text and convert all the vowels to uppercase and all the non-vowels to lowercase.

    Example
    Input
    My Bonnie Lies Over The Ocean

    Output
    my bOnnIE lIEs OvEr thE OcEAn

    I have written the following code but it is not working:
    # include <stdio.h>
    # include <math.h>
    # include <conio.h>
    # include <string.h>
    # include <ctype.h>
    int main (void)
    {
    char str[100], new_str[100];
    int n, nr_vowel, i;
    printf("please input the string\n");getchar("%ch", str);
    n=strlen(str);
    for(i=0;i<n;i++)
    {
    if(str='a'||'e'||'i'||'o'||'u')
    {
    toupper(str);
    }
    else tolower(str);
    }
    printf("%s", str);
    getch();
    return 1;
    }
    if anyone can spot what i am doing wrong i would greatly appreciate it,
    Thanks in advance,
    a very confused noob


Comments

  • Registered Users Posts: 760 ✭✭✭mach1982


    Ok frist there is no space between the include and # ( ie #include <stdio.h>) also try str=='a'||'e'||'i'||'o'||'u'. The == means is equal, = meas that what ever the varible on the left takes the value of the right , ie a=2, a now has the value of of 2.


  • Registered Users Posts: 8,584 ✭✭✭TouchingVirus


    Wow..that code is mess - you need to learn to indent and comment ;)
    # include <stdio.h>
    # include <string.h>
    # include <ctype.h>
    
    int main(int argc, char *argv[])
    {
    	int i;
    
    	printf("Input : &#37;s\n", argv[0]);
    
    	for(i=0;i<strlen(argv[0]);i++)
    	{
    		if((argv[0][i] == 'a') || (argv[0][i] == 'e') || (argv[0][i] == 'i') || (argv[0][i] == 'o') || (argv[0][i] == 'u'))
    		{
    			argv[0][i] = toupper(argv[0][i]);
    		}
    		else
    		{
    			argv[0][i] = tolower(argv[0][i]);
    		}
    	}
    
    	// Print output, wait for character and exit
    	printf("Output : %s\n", argv[0]);
    	return 0;
    }
    

    Since I have a feeling this is an assignment, I'm not going to give you the whole answer. Basically the above program will take in the name of the program (argv[0]) as it's input string, and will do the uppercase/lowercase on it.

    So if you call it "myprogram.exe" the output will be "myprOgrAm.ExE"

    All that remains for you is to figure out how to read in a string of input and change it accordingly.

    Currently your code has things you don't need, like the includes of <conio.h>, <math.h> and the variables nr_vowel and new_str. You return 1 at the end of the program but a non-zero return value tells the OS that the program didn't exit correctly. Use return 0;

    Your conditional (if) statement is wrong, you should be using == but you also should seperate each check (like in my code), I've tried just changing the = to == in my code and everything is read as a vowel then, even consonants and symbols :p

    You are also using getchar to read in your string. Getchar reads in characters one at a time and doesn't take arguments. It does not read in strings so you're already off on the wrong foot. This approach (which can be modified to do what you want) would lead people to type in m<return>y<return>B<return> ..and so on - not very pleasant

    Use scanf to read stuff in..it's a great function ;) *hint - http://www.gnu.org/software/libc/manual/html_node/String-Input-Conversions.html*


  • Closed Accounts Posts: 413 ✭✭sobriquet


    Hi,

    Further to mach1982s' suggestions, you only need to include <stdio.h> and <string.h> for what you're doing; instead of the assignment operation, use the comparison operator '==', and compare them individually like this:
    if ((str[i] == 'a') || (str[i] == 'e') || (str[i] == 'i') || (str[i] == 'o') || (str[i] == 'u'))
    
    As it is that comparison will always evaluate to true.

    Where you're using toupper() and tolower(), you need to do it in this way:
    str[i] = toupper(str[i])
    
    Otherwise nothing will be happening.

    Furthermore, unless I'm mistaken that code shouldn't even compile: getchar() doesn't take any arguments. Use scanf or fgets to read user input.

    Also, your new_str variable is pointless and can be removed, as can that getch() call at the end - should that be getchar() by the way?

    Hope that helps.


  • Registered Users Posts: 8,584 ✭✭✭TouchingVirus


    sobriquet wrote: »
    Hi,

    Further to mach1982s' suggestions, you only need to include <stdio.h> and <string.h> for what you're doing.

    <ctype.h> is necessary too as it is needed for the toupper/tolower methods.
    instead of the assignment operation, use the comparison operator '==', and compare them individually like this:
    if ((str[i] == 'a') || (str[i] == 'e') || (str[i] == 'i') || (str[i] == 'o') || (str[i] == 'u'))
    
    As it is that comparison will always evaluate to true.

    Where you're using toupper() and tolower(), you need to do it in this way:
    str[i] = toupper(str[i])
    
    Otherwise nothing will be happening.

    Correct :)
    Furthermore, unless I'm mistaken that code shouldn't even compile: getchar() doesn't take any arguments. Use scanf or fgets to read user input.

    It doesnt compile :p
    Also, your new_str variable is pointless and can be removed, as can that getch() call at the end - should that be getchar() by the way?

    getch() == getchar() for the most part - see here

    I'll assume it's there because the OP double-clicks on the program, which does it's work and then exits. You can removed getch(); if you intend on running the program from a console window already open, in which case you can see the output.


  • Closed Accounts Posts: 413 ✭✭sobriquet


    Fair enough! I'm just going by sight here, not bothering popping open a terminal window to gcc it. (.exe, pfft.) However, if we're checking each others work:
    <string.h> isn't necessary either. <ctype.h> is though, as it is needed for the toupper/tolower methods.
    I see a strlen() call in both the OPs and your code - that's in string.h. If it's still working regardless of omitting string.h, it might be because stdio or ctype include it themselves.


  • Advertisement
  • Registered Users Posts: 8,584 ✭✭✭TouchingVirus


    Wow...don't I feel...strange :p

    I think MinGW saved me from an error all by itself there :D My original code has <string.h> but when I removed it and compiled it it still works...

    To save on anybody getting confused I'll put it back in and edit my post - nice catch ;)


  • Registered Users Posts: 2,534 ✭✭✭FruitLover


    if(str='a'||'e'||'i'||'o'||'u')

    The reasons this line won't work have been explained above, but anyway, I'd rather use a switch statement here (if you've covered them) for neatness.

    Also, a program that finishes successfully should return 0, not 1 as you have (non-zero return value indicates an error to the OS - not really important for a stand-alone project, but you should know this in case you write programs that will be called from other programs or scripts). <-- oops, just saw that this was mentioned earlier...

    Be careful when you're using input functions - old C code is notorious for being vulnerable to buffer overflow attacks because of careless use of gets(), scanf() and co. If you're using scanf(), make sure you specify how many characters at a maximum you want the function to take in (obviously should be no more than the character array you've allocated can fit). Like so:

    scanf("%100s", str);


  • Registered Users Posts: 23 geistmorder


    Thanks everyone for the help got the program working perfectly now
    Wow..that code is mess - you need to learn to indent and comment
    hmm...you know thats exactly what the lecturer said lol :D
    Since I have a feeling this is an assignment
    actually its not assignment just something i'm practicing with
    that getch() call at the end - should that be getchar() by the way?
    the getch() is there to stop the compiler closing the program after it finishes


  • Registered Users Posts: 8,584 ✭✭✭TouchingVirus


    Thanks everyone for the help got the program working perfectly now

    Excellent stuff - nice work :)


  • Closed Accounts Posts: 413 ✭✭sobriquet


    I think MinGW saved me from an error all by itself there :D My original code has <string.h> but when I removed it and compiled it it still works...
    It must've done - a quick grep of stdio.h and ctype.h on this Ubuntu install says there's no sign of string.h - must be MinGW thing so.
    FruitLover wrote: »
    The reasons this line won't work have been explained above, but anyway, I'd rather use a switch statement here (if you've covered them) for neatness.
    I know what you mean, but unless you call toupper() within each case block, you're relying on the fall through. Looks nice, and I wouldn't be overly concerned, but it's a habit that will bite you in the arse.
    FruitLover wrote: »
    Also, a program that finishes successfully should return 0, not 1 as you have (non-zero return value indicates an error to the OS - not really important for a stand-alone project, but you should know this in case you write programs that will be called from other programs or scripts).
    Indeed. A former boss of mine used to swear up and down that 'normal' return codes from C functions were boolean values - 0 for bad, 1 for good. Talk about throwing my Obsessive Compulsive Disorder switch. Took ages to get him to do the usual -1 (and other negative ints) bad, 0 good, positive ints a return value.


  • Advertisement
Advertisement