Using islower

I want to convrt a sentence to Capital letters
but it outputs
Segmentation fault (core dumped)

Can anyone help me to fix the error..?

this is my output
H
a
v
e

n
i
c
e

d
a
y
yad ecin evaH
Segmentation fault (core dumped)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
bool A;
char name[30]="Have nice day";
char name2[30]="GooD N@iGHt#344 @";

  for(int a=0;a<=strlen(name)-1;a++)
   {
    cout<<name[a]<<endl;
      }

              for(int b=strlen(name)-1;b>=0;b--)
              {
                cout<<name[b];
                }
                  cout<<endl;

strcpy(name,name2);

                     cout<<name;


                    for(int c=0;c<=strlen(name);c++)
                     {
                       A=islower('name[c]');


                         if(A==false);

                      cout<<name[c]-32<<endl;

                        }




return 0;
}
Hi,

Try with this code,


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void ChangeToUpper(char io_String[])
{
    for(int c=0;c<strlen(io_String);c++)
    {                     
       if(islower(io_String[c]))
		io_String[c]=('A'+io_String[c]-'a');		  
    }
}

int main()
{	
	char name[30]="Have nice day";
	char name2[30]="GooD N@iGHt#344 @";

	cout<<"Strings Before Converting"<<std::endl;
	cout<<name<<std::endl;
	cout<<name2<<std::endl;

	ChangeToUpper(name);
	ChangeToUpper(name2);

	cout<<"\n\nStrings After Conversion"<<std::endl;
	cout<<name<<std::endl;
	cout<<name2<<std::endl;

	return 0;
}


Output:
Strings Before Converting
Have nice day
GooD N@iGHt#344 @


Strings After Conversion
HAVE NICE DAY
GOOD N@IGHT#344 @

A=islower('name[c]'); What exactly are you trying to do here? A is a boolean. Also name[c] returns a character so you don't need the single quotes around it.

*Edit One more thing, you should avoid magic numbers like 30.
Last edited on
A=islower('name[c]'); What exactly are you trying to do here? A is a boolean. Also name[c] returns a character so you don't need the single quotes around it.

single quotes are not needed.

'islower' funtion returns true, if the char is lower.
If the passed char is lower, then i am converting into Captital letter.

*Edit One more thing, you should avoid magic numbers like 30.
Can you explain you requirement with some example
What is input and output




'islower' funtion returns true, if the char is lower.
If the passed char is lower, then i am converting into Captital letter.

Whoops I though it said tolower, since that would convert only the alphabetic uppercase letters to lower otherwise return an unchanged letter(well really an int but same difference). Also, it was on his code not yours.

*Edit One more thing, you should avoid magic numbers like 30.
Can you explain you requirement with some example
What is input and output

Not sure what you are trying to say but basically you shouldn't use magic numbers like that. You should be using constant variables like: int const maxLetters = 30;

I would just do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

int main()
{
    std::cout << "Please enter a sentence: ";
    std::string sentence = "";
    std::getline(std::cin, sentence);

    for(char &it : sentence) //you can iterate anyway you want
    //such as for(int i = 0; setence[i]; ++i)
    //or even use std::transform(setence.begin(), setence.end(), sentence.begin(), ::tolower); //or toupper
    {
        it = tolower(it); //or toupper
    }

    std::cout << sentence << std::endl;
    return 0;
}


*edit fixed quote tags
Last edited on
Thanks everyone...!
Topic archived. No new replies allowed.