How to combine a char & a word ?!?
Moderator: Board moderators
How to combine a char & a word ?!?
Hi!
I think this question so simple that ANYBODY can help...
" How to append ONE character to a string? "
For example, we have the word "word", and we want to add an 's'.
I used strcat but it seems like "word" and 's' have a different
gender that can never be combined...
I think this question so simple that ANYBODY can help...
" How to append ONE character to a string? "
For example, we have the word "word", and we want to add an 's'.
I used strcat but it seems like "word" and 's' have a different
gender that can never be combined...
Don't get it...
I don't get your point, sorry... And I'm not familiar with "classes" either.
Isn't there anything more simple, like a ready-to-use command?
I want to add a character after a string, thus:
Thank's,
Raysa
Isn't there anything more simple, like a ready-to-use command?
I want to add a character after a string, thus:
Code: Select all
"word" + 's' = "words"
Raysa
-
- Experienced poster
- Posts: 167
- Joined: Fri Oct 19, 2001 2:00 am
- Location: Saint Petersburg, Russia
It's my mistake or it's not working...
[cpp]#include <stdio.h>
#include <conio.h>
char *s,c;
void strchrcat(char *s, char c)
{
while (*s != 0) s++;
*s = c;
*(s + 1) = 0;
}
void main ()
{
s="word";
c='s';
strchrcat(s,c);
printf ("%s",s);
getch();
}[/cpp]And what if the case is like this:
[cpp]char str[100];
str="MAM";
strcat(str,str[1]); // to make "MAMA"[/cpp]How to fix that?
One more question: How to CLEAR a string after usage to it's
raw-genuine form <"\0\0\0\0\0....">???
Currently I do manually like this:
But it's really a time-wasting, I believe... Is there any instant command?
Thank's in advance,
Raysa
[cpp]#include <stdio.h>
#include <conio.h>
char *s,c;
void strchrcat(char *s, char c)
{
while (*s != 0) s++;
*s = c;
*(s + 1) = 0;
}
void main ()
{
s="word";
c='s';
strchrcat(s,c);
printf ("%s",s);
getch();
}[/cpp]And what if the case is like this:
[cpp]char str[100];
str="MAM";
strcat(str,str[1]); // to make "MAMA"[/cpp]How to fix that?
One more question: How to CLEAR a string after usage to it's
raw-genuine form <"\0\0\0\0\0....">???
Currently I do manually like this:
Code: Select all
for (i=0;i<len;i++)
{
str[i]='\0';
}
Thank's in advance,
Raysa
-
- Learning poster
- Posts: 90
- Joined: Sat Feb 15, 2003 1:39 am
- Location: Paris, France
- Contact:
It doesn't work for two reasons :
Thirst, you didn't allocate any memory for your string :
[c]char *s,c;[/c]
and then this is illequal
[c]s="word";[/c]
and should be replaced by :
[c]strcpy(s,"word");[/c]
This works :
[c]
void strchrcat(char *s, char c)
{
while (*(s++));
*s = c;
*(s + 1) = \0;
}
int main(void)
{
char s[10]="word\0";
char c='s';
strchrcat(s,c);
printf ("[%s]\n",s);
return 0;
}
[/c]
with strcat, you need two char*, but str[1] is a caracter. I think the only way is to use a temp.
[c]
char mama[10]="mam\0";
char temp[2]="!\0";
temp[0]=mama[1];
strcat(mama,temp);
printf("[%s]\n",mama);
[/c]
try this
[c] char string[90]="\0needtoclear";
printf("[%s]\n",string);
[/c]
Thirst, you didn't allocate any memory for your string :
[c]char *s,c;[/c]
and then this is illequal
[c]s="word";[/c]
and should be replaced by :
[c]strcpy(s,"word");[/c]
This works :
[c]
void strchrcat(char *s, char c)
{
while (*(s++));
*s = c;
*(s + 1) = \0;
}
int main(void)
{
char s[10]="word\0";
char c='s';
strchrcat(s,c);
printf ("[%s]\n",s);
return 0;
}
[/c]
with strcat, you need two char*, but str[1] is a caracter. I think the only way is to use a temp.
[c]
char mama[10]="mam\0";
char temp[2]="!\0";
temp[0]=mama[1];
strcat(mama,temp);
printf("[%s]\n",mama);
[/c]
Sure it's time wasting. There is no need to erase all characters. You only need to do str[0]='\0';raysa wrote: One more question: How to CLEAR a string after usage to it's
raw-genuine form <"\0\0\0\0\0....">???
Currently I do manually like this:But it's really a time-wasting, I believe... Is there any instant command?Code: Select all
for (i=0;i<len;i++) { str[i]='\0'; }
try this
[c] char string[90]="\0needtoclear";
printf("[%s]\n",string);
[/c]
Not AC yet
AC at last 


-
- Experienced poster
- Posts: 202
- Joined: Fri Mar 22, 2002 2:00 am
- Location: Chittagong. CSE - CUET
- Contact:
[cpp]
char string_var[100];
/* after using the array just nullify the first slot and the whole string will be
nulled */
string_var[0]=NULL;
[/cpp]
And hey to use string class you don't need to know many advanced thing about classes. like any other variable type use string. Look...
[cpp]
#include<string>
#include<iostream>
int main()
{
string a,b;
a = "I am ";
b = "Moni";
string c = a+b;
cout << c << endl;
return 0;
}
[/cpp]
You will get the output: I am Moni.
Hope it helps
char string_var[100];
/* after using the array just nullify the first slot and the whole string will be
nulled */
string_var[0]=NULL;
[/cpp]
And hey to use string class you don't need to know many advanced thing about classes. like any other variable type use string. Look...
[cpp]
#include<string>
#include<iostream>
int main()
{
string a,b;
a = "I am ";
b = "Moni";
string c = a+b;
cout << c << endl;
return 0;
}
[/cpp]
You will get the output: I am Moni.
Hope it helps


Thank's to both of you: Moni and Bery Olivier.
I've found the string-append operation works really nice now... `_^
But, in nullifying the string, I found it works fine only in Borland C++ v3.1
<under-DOS> but not in 5.02 version <under-Windows>. Why is that?
Btw, consider this <it occurs in Win ver. and may be... Judge's Compiler> :
I have a str[100], and I fill it with "abcde", the str turns from "\0\0\0\0..."
to "abcde\0\0\0\0...". After I do either str[0]=NULL or str[0]='\0', it turns
to "\0bcde\0\0\0\0...". Then I have to fill it with JUST an "a". Instead of
getting the str turns to "a\0\0\0\0\0..." <which I require>, I then get a
"abcde\0\0\0\0..." again!! It's not what I want...
I've solved Power Strings <10298> AC using manual string-cleaning, and got WA
when I replace it with your first-slot-nullifying...
Any idea???
I've found the string-append operation works really nice now... `_^
But, in nullifying the string, I found it works fine only in Borland C++ v3.1
<under-DOS> but not in 5.02 version <under-Windows>. Why is that?
Btw, consider this <it occurs in Win ver. and may be... Judge's Compiler> :
I have a str[100], and I fill it with "abcde", the str turns from "\0\0\0\0..."
to "abcde\0\0\0\0...". After I do either str[0]=NULL or str[0]='\0', it turns
to "\0bcde\0\0\0\0...". Then I have to fill it with JUST an "a". Instead of
getting the str turns to "a\0\0\0\0\0..." <which I require>, I then get a
"abcde\0\0\0\0..." again!! It's not what I want...
I've solved Power Strings <10298> AC using manual string-cleaning, and got WA
when I replace it with your first-slot-nullifying...
Any idea???
-
- Experienced poster
- Posts: 202
- Joined: Fri Mar 22, 2002 2:00 am
- Location: Chittagong. CSE - CUET
- Contact:
Yes it's free under GPL.
Try Here:
http://www.csse.monash.edu.au/coursewar ... reGNU.html
http://www.gnu.org/software/gcc/gcc.html
http://gee.cs.oswego.edu/dl/libg++paper ... ibg++.html
http://www.desy.de/user/projects/C++/g++faq/top.html
The following table may help you for the "long long" type:
64 bit processors have been readily available in workstations for several years, and they will be coming to desktop computers soon. These processors can address enough memory to have arrays with more elements than a 32 bit long can specify. Inexpensive disk drives in use today can hold a file which contains more bytes than a 32 bit offset can reach. The requirement for an integer type required to hold more than 32 bits is obvious.
The 1999 update to the ANSI/ISO C language standard has added a new integer type to C, one that is required to be at contain at least 64 bits.
Included in this update are the new variable types signed and unsigned long long . The selected name comes from gcc and several other compilers which already provide this type as an extension. On 32 bit Windows compilers from Microsoft, Borland (and maybe others) this same extension has the name __int64.
While the C++ standard is quite new and most likely will not change for several years, it is almost certain that C++ compilers will add support for these types as well. The C++ compilers which come with the implementations mentioned above do.
There are two types of long long int, signed and unsigned. If neither is specified the long long is signed. The "int" in the declaration is optional. All 6 of the following declarations are correct:
long long x; x is a signed long long int
long long int x; x is a signed long long int
signed long long x; x is a signed long long int
signed long long int x; x is a signed long long int
unsigned long long x; x is an unsigned long long int
unsigned long long int x; x is an unsigned long long int
The range of the long long int types:
A signed long long can hold all the values between LLONG_MIN and LLONG_MAX inclusive. LLONG_MIN is required to be -9223372036854775807 or less, LLONG_MAX must be at least 9223372036854775807. Again, many 2's complement implementations will define LLONG_MIN to be -9223372036854775808 but this is not required.
An unsigned long long can hold all the values between 0 and ULLONG_MAX inclusive. ULLONG_MAX must be at least 18446744073709551615. The long types must contain at least 64 bits to hold the required range of values.
{form an e-book!}
Try Here:
http://www.csse.monash.edu.au/coursewar ... reGNU.html
http://www.gnu.org/software/gcc/gcc.html
http://gee.cs.oswego.edu/dl/libg++paper ... ibg++.html
http://www.desy.de/user/projects/C++/g++faq/top.html
The following table may help you for the "long long" type:
Table : Byte sizes of GNU C/C++ data types for 64-bit architectures.
Type Bytes Notes
char 1
short 2
int 4
long 8 (4 bytes on 32-bit machines)
long long 8 (may become 16 bytes)
type * 8 (any pointer)
float 4
double 8
long double 8 (may become 10 bytes)
size_t 8 (type of sizeof())
T* - T* 8 (pointer arithmetic)
64 bit processors have been readily available in workstations for several years, and they will be coming to desktop computers soon. These processors can address enough memory to have arrays with more elements than a 32 bit long can specify. Inexpensive disk drives in use today can hold a file which contains more bytes than a 32 bit offset can reach. The requirement for an integer type required to hold more than 32 bits is obvious.
The 1999 update to the ANSI/ISO C language standard has added a new integer type to C, one that is required to be at contain at least 64 bits.
Included in this update are the new variable types signed and unsigned long long . The selected name comes from gcc and several other compilers which already provide this type as an extension. On 32 bit Windows compilers from Microsoft, Borland (and maybe others) this same extension has the name __int64.
While the C++ standard is quite new and most likely will not change for several years, it is almost certain that C++ compilers will add support for these types as well. The C++ compilers which come with the implementations mentioned above do.
There are two types of long long int, signed and unsigned. If neither is specified the long long is signed. The "int" in the declaration is optional. All 6 of the following declarations are correct:
long long x; x is a signed long long int
long long int x; x is a signed long long int
signed long long x; x is a signed long long int
signed long long int x; x is a signed long long int
unsigned long long x; x is an unsigned long long int
unsigned long long int x; x is an unsigned long long int
The range of the long long int types:
A signed long long can hold all the values between LLONG_MIN and LLONG_MAX inclusive. LLONG_MIN is required to be -9223372036854775807 or less, LLONG_MAX must be at least 9223372036854775807. Again, many 2's complement implementations will define LLONG_MIN to be -9223372036854775808 but this is not required.
An unsigned long long can hold all the values between 0 and ULLONG_MAX inclusive. ULLONG_MAX must be at least 18446744073709551615. The long types must contain at least 64 bits to hold the required range of values.
{form an e-book!}

-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
this function is named memset and is used to set given buffer with given charactercode 
Dominik

Dominik
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Born from ashes - restarting counter of problems (800+ solved problems)