please check the code for a question "Write a program to check whether the given string is palindromic or not."
#include %26lt;iostream.h%26gt;
#include %26lt;conio.h%26gt;
void main
{ char x[100];
char y[100];
int temp1,temp2;
int ans = 0;
cout %26lt;%26lt; "Enter the word ";
cin %26gt;%26gt; x;
cout %26lt;%26lt; "Enter the word ";
cin %26gt;%26gt; y;
temp1 = strlen(x);
temp2 = strlen(y);
for(int k = 0;k %26lt; temp1 ;k--)
{for(int j = temp2;j %26lt;= ......;j--)
{if(x[k] == j[k])
ans = 1;
continue;
else
ans = 0;
}}
if(ans == 1)
cout %26lt;%26lt; "The string is palindromic";
else
cout %26lt;%26lt; "The string is not palindromic";
getch();
}
please make the necessary corretions as i dont have a compiler please help!!!!!!!!
C++ program to check whether the string is palindromic or not?
#include %26lt;iostream%26gt;
#include %26lt;string%26gt;
using namespace std;
int main()
{
char str[100];
cout %26lt;%26lt; "Enter word :";
cin %26gt;%26gt; str;
int x = strlen(str)-1;
for(int i = 0; i %26lt;= x; i++)
{
if (str[i] == str[x-i])
{
continue;
}
else
{
cout%26lt;%26lt;"Not a palidrome"%26lt;%26lt;endl;
return 0;
}
}
cout %26lt;%26lt; "Indeed Palidrome"%26lt;%26lt;endl;
return 0;
}
Reply:Your code is messed up and I don't really understand your logic, if you want to chec whether a string is palindromic, you need only enter one string, not 2.
if it helps, this is how I'd do it, although it is written in C, you should not have much trouble understanding it, specially if you use google.
#include %26lt;stdio.h%26gt;
#include %26lt;string.h%26gt;
#include %26lt;ctype.h%26gt;
void trimSpaces(char *buffer);
int main()
{
char buffer[1024];
int start_ctr, end_ctr;
int is_pal;
fgets(buffer, 1023, stdin);
trimSpaces(buffer);
printf("After trim: %s\n", buffer);
end_ctr = strlen(buffer) - 1;
start_ctr = 0;
is_pal = 1;
while( (start_ctr != end_ctr) %26amp;%26amp; (end_ctr %26gt; start_ctr) )
{
if( tolower(buffer[start_ctr]) != tolower(buffer[end_ctr]) )
{
is_pal = 0;
break;
}
start_ctr++;
end_ctr--;
}
if( is_pal )
{
printf("Palindromic, indeed :)\n");
}
else
{
printf("Not palindromic :(\n");
}
return 0;
}
void trimSpaces(char *buffer)
{
int i = 0, j = 0;
for(; buffer[i] != '\0'; i++ )
{
if( isalpha(buffer[i]) )
{
buffer[j] = buffer[i];
j++;
}
}
buffer[j] = '\0';
}
Anobody besides me thinks yahoo needs a CODE tag?
Reply:Hi I study in DPS Ahmedabad if you know me please contact me(i will give you the compiler). This Pogramme will definately work for palindrome check
#include %26lt;iostream.h%26gt;
#include %26lt;conio.h%26gt;
#include %26lt;string.h%26gt;
void main()
{
clrscr();
char x[100];
char y[100];
int temp1,temp2=0;
cout %26lt;%26lt; "Enter the word ";
cin %26gt;%26gt; x;
temp1 = strlen(x)-1;
for(int j= 0;j%26lt;strlen(x);j++)
{
y[j] = x[temp1 - j];
}
y[j] = '\0';
x[j] = '\0';
for(int k =0; k%26lt;strlen(x); k++)
{
if (x[k]==y[k]){
temp2 = temp2 + 1;
}
}
if (strlen(x) == temp2)
{cout%26lt;%26lt;"Palindrome";
}
else
{cout%26lt;%26lt;"not a palindrome";
}
getch();
}
Thursday, July 30, 2009
In C++:How do you make a STRING uppercase?
I am working with a string being passed in from a client. I am comparing this string to a preset value.
In order to do this, I need to make the incomming string capital. I know toupper will only work on char.
How can I make a string all uppercase?
In C++:How do you make a STRING uppercase?
Not as such, but you can write one without much trouble.
void stoupper(std::string%26amp; s)
{
std::string::iterator i = s.begin();
std::string::iterator end = s.end();
while (i != end) {
*i = std::toupper((unsigned char)*i);
++i;
}
}
Reply:One other oddball suggestion. If you can store the preset value in a string, you can use the built in function compare. More information at the URL below. Pivy.
In order to do this, I need to make the incomming string capital. I know toupper will only work on char.
How can I make a string all uppercase?
In C++:How do you make a STRING uppercase?
Not as such, but you can write one without much trouble.
void stoupper(std::string%26amp; s)
{
std::string::iterator i = s.begin();
std::string::iterator end = s.end();
while (i != end) {
*i = std::toupper((unsigned char)*i);
++i;
}
}
Reply:One other oddball suggestion. If you can store the preset value in a string, you can use the built in function compare. More information at the URL below. Pivy.
Read specific pattern string from a file in C?
My file is something like 1 10 20 308
How can I read each value individually and store in a int or string array?
Read specific pattern string from a file in C?
all info stored in a file is stored in "char" (8 bit) format, so you would have to know which numbers were originally integers or strings (or whatever). and read them out the way they were written in.
your example 1 10 20 308:
would be stored in the file like (in HEX to make it easier):
0x01 0x0A 0x14 0x01 0x34
there isn't any way to tell what this represents unless you know how they were stored.
it could be all char which would mean 1 10 20 1 52
Reply:I believe you'd use something like this:
fscanf(filePointer, "%d", %26amp;intArray)
in a loop until fscanf returns "null" for end-of-file.
How can I read each value individually and store in a int or string array?
Read specific pattern string from a file in C?
all info stored in a file is stored in "char" (8 bit) format, so you would have to know which numbers were originally integers or strings (or whatever). and read them out the way they were written in.
your example 1 10 20 308:
would be stored in the file like (in HEX to make it easier):
0x01 0x0A 0x14 0x01 0x34
there isn't any way to tell what this represents unless you know how they were stored.
it could be all char which would mean 1 10 20 1 52
Reply:I believe you'd use something like this:
fscanf(filePointer, "%d", %26amp;intArray)
in a loop until fscanf returns "null" for end-of-file.
How do i read in arrays of numbers and streams, using c strings in c++?
Im writing a census program that requires me to use c string objects to read in three things, names(states), population, and area. What I dont know and cant find in my text book, at least in a clear way, is how to read numbers and names from a text file saved separately in my directory. My objects are being declared as ifstream and ofstream.
How do i read in arrays of numbers and streams, using c strings in c++?
If you mean how to read them from the text file, ifstream and ofstream work just like any other stream, such as cin and cout:
ifstream read("census.txt")
string name
int population,area
read %26gt;%26gt; name
read %26gt;%26gt; population
read %26gt;%26gt; area
wedding reception flowers
How do i read in arrays of numbers and streams, using c strings in c++?
If you mean how to read them from the text file, ifstream and ofstream work just like any other stream, such as cin and cout:
ifstream read("census.txt")
string name
int population,area
read %26gt;%26gt; name
read %26gt;%26gt; population
read %26gt;%26gt; area
wedding reception flowers
In Visual C++, how do you reverse a string?
the code that i have so far is:
#include "stdafx.h"
#include %26lt;iostream%26gt;
#include %26lt;iomanip%26gt;
#include %26lt;string%26gt;
using namespace std;
string Reverse(string);
int _tmain(int argc, _TCHAR* argv[])
{
string data = "Hello There";
cout %26lt;%26lt; "The reverse of " %26lt;%26lt; data %26lt;%26lt; " is " %26lt;%26lt; Reverse(data) %26lt;%26lt; endl;
system("PAUSE");
return 0;
}
string Reverse(string x) {
int counter;
string NewString(x);
//enter something here
return NewString;
}
In Visual C++, how do you reverse a string?
reverse( NewString.begin(), NewString.end() );
(You will have to #include %26lt;algorithm%26gt; )
Reply:const string %26amp; reverse( string %26amp; s )
{
return (s = strrev( s.c_str()));
}
Reply:easy... from the top of my head (might need some changes in slight syntax, but heres the idea)
char temp;
for (int x = 0 ; x %26lt; NewString.length() ; x++)
{
//swap two characters
temp = NewString[x];
NewString[x] = NewString[NewString.length()-x ];
NewString[ NewString.length()-x ] = temp;
}
The only things im not sure about are .length(), whether [x] on a string is the right syntax to get a single character, and whether temp should be a string or a char. good luck
#include "stdafx.h"
#include %26lt;iostream%26gt;
#include %26lt;iomanip%26gt;
#include %26lt;string%26gt;
using namespace std;
string Reverse(string);
int _tmain(int argc, _TCHAR* argv[])
{
string data = "Hello There";
cout %26lt;%26lt; "The reverse of " %26lt;%26lt; data %26lt;%26lt; " is " %26lt;%26lt; Reverse(data) %26lt;%26lt; endl;
system("PAUSE");
return 0;
}
string Reverse(string x) {
int counter;
string NewString(x);
//enter something here
return NewString;
}
In Visual C++, how do you reverse a string?
reverse( NewString.begin(), NewString.end() );
(You will have to #include %26lt;algorithm%26gt; )
Reply:const string %26amp; reverse( string %26amp; s )
{
return (s = strrev( s.c_str()));
}
Reply:easy... from the top of my head (might need some changes in slight syntax, but heres the idea)
char temp;
for (int x = 0 ; x %26lt; NewString.length() ; x++)
{
//swap two characters
temp = NewString[x];
NewString[x] = NewString[NewString.length()-x ];
NewString[ NewString.length()-x ] = temp;
}
The only things im not sure about are .length(), whether [x] on a string is the right syntax to get a single character, and whether temp should be a string or a char. good luck
When I used string data type in my c++ programme, it shows error i.e. data type expected.?
as per i know, string is also a data type, but when i declare variable with string datatype, error is data type expected, variable not declared , ect.
so, for alternative, i used char datatype, but it accepts character till space %26amp; ignore(or assign to next variable) character entered after space, plzzzzzzzzzz help me
When I used string data type in my c++ programme, it shows error i.e. data type expected.?
Are u sure ur working in c++ not vc++? In c++ string is NOT data type i am damn sure. If ur reading the chars with cin function it reads only till blank space and thinks end of string so u wont get anything after space. Instead u use getche() function. This program is nothing to do with string.h. include string.h for strcmp,strlen etc function if required.
Reply:There Is U Haven't Included #include%26lt;string.h%26gt; Else There Is No Error,Means U May Haven't Included..
Reply:If you are using the STL string class then you probably want to add at the top:
#include %26lt;string%26gt;
and:
using namespace std;
Reply:Hmm...do you have to #include string.h?
The char datatype only takes one character. If you're looking to store words or phrases, you should use a string. What does the error message say exactly, and what syntax are you trying to use to declare the string?
so, for alternative, i used char datatype, but it accepts character till space %26amp; ignore(or assign to next variable) character entered after space, plzzzzzzzzzz help me
When I used string data type in my c++ programme, it shows error i.e. data type expected.?
Are u sure ur working in c++ not vc++? In c++ string is NOT data type i am damn sure. If ur reading the chars with cin function it reads only till blank space and thinks end of string so u wont get anything after space. Instead u use getche() function. This program is nothing to do with string.h. include string.h for strcmp,strlen etc function if required.
Reply:There Is U Haven't Included #include%26lt;string.h%26gt; Else There Is No Error,Means U May Haven't Included..
Reply:If you are using the STL string class then you probably want to add at the top:
#include %26lt;string%26gt;
and:
using namespace std;
Reply:Hmm...do you have to #include string.h?
The char datatype only takes one character. If you're looking to store words or phrases, you should use a string. What does the error message say exactly, and what syntax are you trying to use to declare the string?
In C++ how do you convert a string to enum?
Lets say I have the code
Code:
enum sKind_t { S_BLUE, S_GREEN, S_YELLOW };
and
Code:
struct square_t { sKind_t kind; string name; };
And Had a file Like this
BLUE apple
GREEN square
GREEN apple
YELLOW Horse
You then write some code to split up the file into two seperrate strings for each line
example
string A= BLUE
string B = apple
how do you store the enum value via a string
so i want say
square_t.kind = A;
but this doesn't work
how do you get strings that are one of the correct values for an enum to work as an enum category?
you change string A to "S_" + A giving S_BLUE
In C++ how do you convert a string to enum?
As the other answerer has said you cannot directly convert from string to enum. What you can do is have a mapping between the strings and the enum. One simple way would be comparing the inputted string with a colour like BLUE, YELLOW, GREEN. If the string is one of these, then use the enum value of it, like S_BLUE, S_YELLOW, S_GREEN.
I do not understand what you are trying to do, but this may help.
Reply:Enum types are named integer constants. You cannot directly convert them to C-style strings, or vice versa.
Reply:I think you need a dictionary. You can use the STL %26lt;map%26gt; class which operates in logarithmic time or for constant time you can use a hash table.
Code:
enum sKind_t { S_BLUE, S_GREEN, S_YELLOW };
and
Code:
struct square_t { sKind_t kind; string name; };
And Had a file Like this
BLUE apple
GREEN square
GREEN apple
YELLOW Horse
You then write some code to split up the file into two seperrate strings for each line
example
string A= BLUE
string B = apple
how do you store the enum value via a string
so i want say
square_t.kind = A;
but this doesn't work
how do you get strings that are one of the correct values for an enum to work as an enum category?
you change string A to "S_" + A giving S_BLUE
In C++ how do you convert a string to enum?
As the other answerer has said you cannot directly convert from string to enum. What you can do is have a mapping between the strings and the enum. One simple way would be comparing the inputted string with a colour like BLUE, YELLOW, GREEN. If the string is one of these, then use the enum value of it, like S_BLUE, S_YELLOW, S_GREEN.
I do not understand what you are trying to do, but this may help.
Reply:Enum types are named integer constants. You cannot directly convert them to C-style strings, or vice versa.
Reply:I think you need a dictionary. You can use the STL %26lt;map%26gt; class which operates in logarithmic time or for constant time you can use a hash table.
Subscribe to:
Posts (Atom)