C questions. Pleas esend me this as early as possible
Regards
Varun
C: Why '%26amp;' not used to declare String or Character?
in c,%26amp; is used as a 'address of' operator,hence cannot be used to declare a character ,string and to declare u can use a pointer.
Reply:%26amp; symbol is a special symbol. You can give as a value for a string or a character. But any variable declartion should not start with symbols other than _(underscore). Usually %26amp; is used both as reference operator as well as "address operator". So it cant be used. Hope u understoold this.
Reply:%26amp; is a special charecter or symbol like $ # used as shortcuts
Reply:string in C is an array.
the symbol %26amp; can be represented as an character
char a='%26amp;';
a string example
char s[]="c%26amp;language";
Reply:%26amp; is address of operator in C
Thursday, July 30, 2009
C++ programming, editing function to return string instead of char.?
This function used to return a single letter. How can I change it so it could return a string of 6 characters?
char pop(stack *s)
{
char pop;
pop=s-%26gt;cars[s-%26gt;top];
s-%26gt;cars[s-%26gt;top]='\0';
s-%26gt;top--;
return pop;
}
C++ programming, editing function to return string instead of char.?
Hi,
The variable name pop and function name cannot be same.
I suppose you are having a structure stack. something like this:
struc stack
{
char cars[10][7];
int top;
};
Also here I am considering u are using C language and want to store 10 cars each of whose name is of at the most of 6 characters.
char* pop_operation(stack *s)
{
char *pop;
pop=(char *)malloc(sizeof(char)*7); // In C++, use
// pop =new char[7];
strcpy(pop,s-%26gt;cars[s-%26gt;top]);
s-%26gt;cars[s-%26gt;top]='\0';
s-%26gt;top--;
return pop;
}
Where ever u r calling this function, note u have to declare a string pointer before by writing,
char *str;
and at the time of call as
str=pop_operation(strucure variable or pointer as u did earlier);
U can print it as u print other strings.
In case of further more clarifications, u may query me again.
OM NAMAH SHIVAY
Reply:What you are wanting to return is a C-String, or character array. Simply declare char pop as a array of 6 characters
char pop[5];
then fill the values into the pop array, and return the value when your done with the function.
char pop(stack *s)
{
char pop;
pop=s-%26gt;cars[s-%26gt;top];
s-%26gt;cars[s-%26gt;top]='\0';
s-%26gt;top--;
return pop;
}
C++ programming, editing function to return string instead of char.?
Hi,
The variable name pop and function name cannot be same.
I suppose you are having a structure stack. something like this:
struc stack
{
char cars[10][7];
int top;
};
Also here I am considering u are using C language and want to store 10 cars each of whose name is of at the most of 6 characters.
char* pop_operation(stack *s)
{
char *pop;
pop=(char *)malloc(sizeof(char)*7); // In C++, use
// pop =new char[7];
strcpy(pop,s-%26gt;cars[s-%26gt;top]);
s-%26gt;cars[s-%26gt;top]='\0';
s-%26gt;top--;
return pop;
}
Where ever u r calling this function, note u have to declare a string pointer before by writing,
char *str;
and at the time of call as
str=pop_operation(strucure variable or pointer as u did earlier);
U can print it as u print other strings.
In case of further more clarifications, u may query me again.
OM NAMAH SHIVAY
Reply:What you are wanting to return is a C-String, or character array. Simply declare char pop as a array of 6 characters
char pop[5];
then fill the values into the pop array, and return the value when your done with the function.
In C++ how can enhance my text (string) output?
i need the code to enhance a text which i read it from consol and saved as string,how can i print this text to:
* Make one space and only one space between each word and another.Each sentence separator (ie. ? , ! , ',', ';' , ' .') should not have a space before it and should have only once after it. (and i don't know if it possible to start the word after separator with capital letter ).
EX:
string mystr;
mystr(cin.'/n');
// supose i read the string as :
hi how?are you?! im fine.
after reading i want to print (mystr) the text as:
Hi how? Are you?! Im fine.// look at spaces "only one space between each word" and notic the capital letter after seperator (ex. how? Are..)
In C++ how can enhance my text (string) output?
The trick behind this question is doing things in the right order.
1) Start with replacing each of the sentence separators for the same character followed by a space.
2) Next, replace all double spaces for a single space, and repeat this for as long as there are double spaces in the string.
3) Replace each combination of a space followed by any of the sentence separators for the sentence separators (in other words, remove any spaces before a sentence separator).
4) If they exist, remove the space at the beginning of the sentence and the space at the end of a sentence. Because of step 2, there can only be (at most) one space at the beginning or end.
5) If the last character isn't a period, add a period at the end.
6) Capitalize the first character of the string.
7) Look for sentence separators that are followed by a space; capitalize the character that follows the space.
I think that's it. I hope it helps.
PS: 'replace' is discussed on page 595 of Stroustrup's book.
* Make one space and only one space between each word and another.Each sentence separator (ie. ? , ! , ',', ';' , ' .') should not have a space before it and should have only once after it. (and i don't know if it possible to start the word after separator with capital letter ).
EX:
string mystr;
mystr(cin.'/n');
// supose i read the string as :
hi how?are you?! im fine.
after reading i want to print (mystr) the text as:
Hi how? Are you?! Im fine.// look at spaces "only one space between each word" and notic the capital letter after seperator (ex. how? Are..)
In C++ how can enhance my text (string) output?
The trick behind this question is doing things in the right order.
1) Start with replacing each of the sentence separators for the same character followed by a space.
2) Next, replace all double spaces for a single space, and repeat this for as long as there are double spaces in the string.
3) Replace each combination of a space followed by any of the sentence separators for the sentence separators (in other words, remove any spaces before a sentence separator).
4) If they exist, remove the space at the beginning of the sentence and the space at the end of a sentence. Because of step 2, there can only be (at most) one space at the beginning or end.
5) If the last character isn't a period, add a period at the end.
6) Capitalize the first character of the string.
7) Look for sentence separators that are followed by a space; capitalize the character that follows the space.
I think that's it. I hope it helps.
PS: 'replace' is discussed on page 595 of Stroustrup's book.
How do you print out the value in tstamp_t in C? Is it a string? Long?... Or do you have to break it apart?
Seems that you are asking from a reference guide. Probably that might be a variable the author used. Refer the previous pages of the book. The program should be carrying over from previous pages. Or try to give a piece of code here.
How do you print out the value in tstamp_t in C? Is it a string? Long?... Or do you have to break it apart?
The short answer: how are we suppose to know how to print it if we don't know what it is? It probably is some kind of time stamp but without the definition we can't know if it is a struct a typedef of some basic type or something else. Did it out and then someone can help you print it.
bridal flowers
How do you print out the value in tstamp_t in C? Is it a string? Long?... Or do you have to break it apart?
The short answer: how are we suppose to know how to print it if we don't know what it is? It probably is some kind of time stamp but without the definition we can't know if it is a struct a typedef of some basic type or something else. Did it out and then someone can help you print it.
bridal flowers
C++ program to check whether the string is palindromic or not?
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();
}
#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();
}
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.
Subscribe to:
Posts (Atom)