3 Replies - 619 Views - Last Post: 06 May 2014 - 03:53 PM Rate Topic: -----

#1 michelldee20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-April 14

Help I can't seem to get this program to work

Posted 06 May 2014 - 02:59 PM

//Michell Leech
//05/04/2014
//Program to compare strings or convert characters to upper/lower case

#include <stdio.h>
#include<string.h>
#include<ctype.h>
//declares functions
void convertToUppercase(char *s3);
void convertToLowercase(char *s3);
//main function
int main(void){
//initializes variables, arrays and size of arrays.
int option;
char string1[20];
char string2[20];
int result;
char string;
char i;
char string3[20];
char *s3 = string3[20];//sets pointer to point at array




    do{
        printf("Enter option 1 to compare strings \n\nEnter option 2 to convert strings to upper/lower case, \n\nThen press enter   Press -1 to terminate");
        scanf("%d", &option);

        if(option == 1){

            printf("Enter 2 strings       ");
            scanf("%19s%19s", &string1, &string2);

            result = strcmp(string1,string2);

            if(result > 0){
                printf("%s is greater than %s  ", string1, string2);
            }
                else if(result = 0){
                    printf("%s is equal to %s  ", string1, string2);
                }
                else{
                    printf("%s is less than %s  ", string1, string2);
                }


            }


        else if(option == 2){

            printf("\nEnter a string in upper or lower case then press enter");
            scanf("%20c", &string);
            string = string3[20];

            if(islower(string)){

            convertToUppercase(string3);

            printf("Uppercase String is %s \n", string);
            }
            else{
            convertToLowercase(string3);

            printf("Lowercase String is %s \n", string);

        }
    }

    while(option != -1);



system("pause");
}

void convertToUppercase(char *s3){

    while(*s3 != '\0'){

        *s3 = toupper(*s3++);
    }
}

void convertToLowercase(char *s3){

    while(*s3 != '\0'){

    *s3 = tolower(*s3++);
    }
}





This post has been edited by modi123_1: 06 May 2014 - 03:04 PM
Reason for edit:: fixed botched code tag


Is This A Good Question/Topic? 0
  • +

Replies To: Help I can't seem to get this program to work

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,847
  • Joined: 12-June 08

Re: Help I can't seem to get this program to work

Posted 06 May 2014 - 03:05 PM

Quote

I can't seem to get this program to work

Care to elaborate? Error messages' text? behavior that is not as expected? Etc.
Was This Post Helpful? 0
  • +
  • -

#3 michelldee20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-April 14

Re: Help I can't seem to get this program to work

Posted 06 May 2014 - 03:11 PM

The option 2 part of the program will not convert the string to upper or lowercase.

This are the errors I'm getting.

21:12: warning: initialization makes pointer from integer without a cast [enabled by default]

57:13: warning: passing argument 1 of 'islower' makes integer from pointer without a cast [enabled by default]

53:37: note: expected 'int' but argument is of type 'char *'
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon

Reputation: 5334
  • View blog
  • Posts: 16,602
  • Joined: 25-December 09

Re: Help I can't seem to get this program to work

Posted 06 May 2014 - 03:53 PM

So let's start looking at the lines in question:

Line 21:
char *s3 = string3[20];//sets pointer to point at array

What exactly are you trying to do in this line? Why are you even trying to do this? You never use s3 in main so just delete the line.

Next:
            scanf("%20c", &string);
            string = string3[20];

            if(islower(string)){


Do you realize that string is a single character, not an array?

What are you trying to do with the second line above? What you're actually doing is accessing string3 out of bounds. Remember arrays in C start at zero and stop at the size of the array minus one.

Next let's look at this line:

          scanf("%19s%19s", &string1, &string2);

When dealing with C-strings and scanf you don't need the ampersand, the name of the variable is enough.


It looks like you really need to review your text about arrays and the printf() and scanf() functions.

Also in future please post the complete compiler error/warning messages exactly as they appear in your development environment. These messages have important information embedded within them and the order of the messages is also very important.


Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1