user2203774
user2203774

Reputation: 619

Pointer and Array in C

I'm new to C and programming. I got stuck at a homework exercise. My output only shows the first character in upper case, and the following characters in some weird numbers. Can someone take a look at my code and give me some tips on what I've done wrong and ways to fix the issue? Your help is greatly appreciated!

"Write a function void sticky(char* word) where word is a single word such as “sticky” or “RANDOM”. sticky() should modify the word to appear with “sticky caps” (http://en.wikipedia.org/wiki/StudlyCaps), that is, the letters must be in alternating cases(upper and lower), starting with upper case for the first letter. For example, “sticky” becomes “StIcKy” and “RANDOM” becomes “RaNdOm”. Watch out for the end of the string, which is denoted by ‘\0’. You can assume that legal strings are given to the sticky() function."

#include <stdio.h>
#include <stdlib.h>

/*converts ch to upper case, assuming it is in lower case currently*/
char toUpperCase(char ch)
{
 return ch-'a'+'A';
}

/*converts ch to lower case, assuming it is in upper case currently*/
char toLowerCase(char ch)
{
 return ch-'A'+'a';
}

void sticky(char* word){
 /*Convert to sticky caps*/

for (int i = 0; i < sizeof(word); i++)
{
    if (i % 2 == 0)
    {
        word[i] = toUpperCase(word[i]);
    }
    else
    {
        word[i] = toLowerCase(word[i]);
    }
}

}

int main(){
/*Read word from the keyboard using scanf*/
char word[256];
char *input;
input = word;
printf("Please enter a word:\n");
scanf("%s", input);

/*Call sticky*/
sticky(input);

/*Print the new word*/
printf("%s", input);

for (int i = 0; i < sizeof(input); i++)
{
    if (input[i] == '\n')
    {
        input[i] = '\0';
        break;
    }
}

return 0;

}

Upvotes: 2

Views: 1375

Answers (7)

user2203774
user2203774

Reputation: 619

Thank you so much for the tips! Using your suggestions, I modified my code and it's working now.

Below is my revised code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*converts ch to upper case, assuming it is in lower case currently*/
char toUpperCase(char ch){
    return ch-'a'+'A';
}

/*converts ch to lower case, assuming it is in upper case currently*/
char toLowerCase(char ch){
    return ch-'A'+'a';
}



void sticky(char* word)
{
 /*Convert to sticky caps*/

for (int i = 0; i < strlen(word); i++)
{
    if (i % 2 == 0)
    {
        if (word[i] >= 'a' && word[i] <= 'z')
        {
            word[i] = toUpperCase(word[i]);
        }

    }
    else
    {
        if (word[i] >= 'A' && word[i] <= 'Z')
        {
            word[i] = toLowerCase(word[i]);
        }

    }
}

}

int main(){
/*Read word from the keyboard using scanf*/
char word[256];
char *input;
input = word;
printf("Please enter a word:\n");
scanf("%s", input);

/*Call sticky*/
sticky(input);

/*Print the new word*/
printf("%s", input);

for (int i = 0; i < sizeof(input); i++)
{
    if (input[i] == '\n')
    {
        input[i] = '\0';
        break;
    }
}

return 0;

}

Upvotes: 0

Ritesh Kumar Gupta
Ritesh Kumar Gupta

Reputation: 5191

Modify your change upper and change lower function

/*converts ch to upper case,*/
char toUpperCase(char ch)
{
    if(ch>='a' && ch<='z')/*If condition just to make sure current letter is in  lower case*/
        return ch-'a'+'A';
}

/*converts ch to lower case, assuming it is in upper case currently*/
char toLowerCase(char ch)
{
    if(ch>='A' && ch<='Z')/*If condition just to make sure current letter is in  Upper case*/
        return ch-'A'+'a';
}

Also, only four characters are converted since you are using sizeof for finding the string length.sizeof always returns 4(depends upon machine). use strlen(word) to find the length of string word in following for loop:

for (int i = 0; i < strlen(word); i++)
{
}

Upvotes: 3

Fabien
Fabien

Reputation: 13406

Something is wrong in your code : you are making odd characters upper case and even ones lower but you do no check whether they were lower or upper case in the first place. But lowering an already lower-case letter gives you a wrong value (the same is true for "uppering" an already upper-case letter).

So you should do :

char toUpperCase(char ch)
{
 if ((ch >= 'a') && (ch <= 'z')) {
   return ch-'a'+'A';
 } else {
   return ch;
 }
}

and the same for toLowerCase.

Upvotes: 0

You should use strlen instead of sizeof. Also, you must check whether your letter is already upper or lower case:

for (int i = 0; i < strlen(word); i++)
{
    if (i % 2 == 0)
    {
        if ( isLowerCase(word[i]) )
        {
            word[i] = toUpperCase(word[i]);
        }
        else
        {
            // do nothing.
        }
    }
    else
    {
        if ( isUpperCase(word[i]) )
        {
            word[i] = toLowerCase(word[i]);
        }
        else
        {
            // do nothing.
        }
    }
}

Note that I haven't implemented the isUpperCase and isLowerCase functions ;D

Upvotes: 2

Keith Nicholas
Keith Nicholas

Reputation: 44288

you need to use strlen not sizeof to find the length of a char* string

Upvotes: 5

jbgs
jbgs

Reputation: 2885

sizeof (word) is the size of a char *, you must pass another parameter with the array size... or use strlen ().

Upvotes: 0

kamituel
kamituel

Reputation: 35950

Function sizeof() is used to calculate the size of the datatype, not the size allocated to the pointer.

So you can not use it like sizeof(word). Instead, iterate over a characters until you stumble upon a \0, which indicates end of string.

On example:

int i = 0;
while ( word[i] != 0 ) {
  // do lower/upper case conversion.
}

Upvotes: 0

Related Questions