0
int main()
{
    cout<<"Enter a word"<<endl;
    char word1[]={0}; //first char array initialization
    cin>>word1;
    cout<<"Enter another word"<<endl;
    char word2[]={0};  //second char array initialization
    cin>>word2;
    char word3[]={0};  
    char word4[]={0};
    int i=0;
while (word1[i]!='\0')  //this converts both words to lower case by usinction tolower 
{
    word3[i]=char(tolower(word1[i])); //word3 and word4 stores the new arrays
    word4[i]=char(tolower(word2[i]));
    i++;
}

int output;  //output stores the value of 0,1 or -1
output=compareString(word3,word4);
if (output==0)
{
    cout<<"The two words are the same."<<endl; //if arrays are same
}
if (output==1)  
{
    cout<<"the 1st character of 1st word has a larger value than of 2nd word."<<endl;
}
if (output==-1)
{
    cout<<"the 1st character of 2nd word has a larger value than of 1st word."<<endl;
}
return 0;

}

int compareString(char string1,char string2)
{
    int size1=0;  //initialize size of string1
    int j=0;   //string1 position initialize
    while (string1[j]!='\0')  //loop to determine size of string1
    {
        size1+=1;
        j+=1;
    }
    int a=0;    //initialize size of string2
    int size2=0;  //string2 position
    while (string2[a]!='\0')  //loop determines size of string2
    {
        size2+=1;
        a+=1;
    }
     int i=0;
     int k=0;
     for (i=0;i<size1;i++)   //loop to compare the two strings
     {
     if (string1[i]!=string2[i])
     {
        if (string1[i]>string2[i])  //comparing 1st character of string1 & string2
        {
            return 1;
        }    
        else   //if string2's first character is greater in value
        {
            return -1;
        }
      }
      else
      {
          k++;  //incrementing k when character of string1 matches string2 character
      }
      }
   if (k==size1)  //to cjheck if all characters of both strings are same
   {
       if (k==size2)
       {
           return 0;
       }
   }
 }

This is a function which compares two char arrays and returns 0 if characters correspond to each other,returns 1 if first character of string1 is greater than first character of string2 in value and returns -1 if first character of string1 is less than first character of string2.The problem is that when i run it,even when the two words are different,the output is always 0 and the text "The words are the same" appears. Am i initializing the two arrays correctly in my main program?or is there some other problem?

7
  • 1
    Why haven't you given the char arrays size?? Commented Oct 8, 2012 at 7:01
  • Your arrays are one element. Use std::string if you can. Not to kill the point of the function, but it has a compare function that does that as well as specific operators like == and >. Commented Oct 8, 2012 at 7:01
  • @Troy, If you provide the initializer list, it deduces the size. Commented Oct 8, 2012 at 7:02
  • Yep, not going to be able to do much with a single character array terminates with a null character! Commented Oct 8, 2012 at 7:03
  • I have to use char.it is an assignment in which i have to make a function which compares two character arrays. Commented Oct 8, 2012 at 7:05

2 Answers 2

2

This declaration

char word1[]={0};

declares an array of size 1, meaning when you do your input it will overwrite the stack. The same for all other arrays.

When dealing with string in C++ it is highly recommended to use std::string!

Sign up to request clarification or add additional context in comments.

4 Comments

how can I initialize an array where the user determines the size of array?
I mean which doesnot have a constant size
@user1692446 for strings, std::string. for other data std::vector.
@user1692446 Ah, I see this is an assignment. Then either use large arrays, or pointers and allocate/reallocate as needed.
2
char word1[]={0};

This line creates an array word1 that has exactly one element, set to 0. When using this array to hold a string, you cannot hold anything in this except the empty string. You are causing a buffer overflow here, because you read a non-empty string into this array, which will then write into other parts of memory not allocated to this array. This is very bad.

Consider using std::string to hold strings instead. It will automatically resize its allocation as necessary.

2 Comments

I have to use character arrays according to my assignment.
Then make them really large, like 1024 elements each. Larger than any string that would usually be entered into the program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.