View Full Version : delete the second character of a string?
batteur91
28 janvier 2004, 16h42
Hi all,
I'm looking for a solution to retrieve ( or copy) the second character of a string.
My program is:
int iNum=0;
char sNum[2]="";
//Somewhere in the program, the integer iNum=92
// Then
sprintf (sNum,"%d",iNum);
And I want to delete the first character of the string sNum, but i don't know how can i do?
Or I want to retrieve the second character of the string sNum which is the value '2' in order to put it to another string, but i don't know how can i do?
In sumarize, I want that sNum="2"; or another string ="2"; I don't want to get the whole value iNum. I only want the last value of it.
Thanks for your help
Bye
batteur91
Matt
28 janvier 2004, 17h22
1) You are on a FRENCH forum !!!
2) When you write in english, please try to use correct english grammar and syntax ...
Spycam
28 janvier 2004, 17h49
And what is the language ? C++ ?
batteur91
28 janvier 2004, 18h02
I use the C language.
Cougar
28 janvier 2004, 18h42
main()
{
char sNum[1],res;
/*sNum = "bo"*/
sNum[0]='b';
sNum[1]='o';
/* res is the second character of sNum*/
res=sNum[1];
}
batteur91
28 janvier 2004, 20h53
thanks but, this is not enough,
In my program, sNumis eqaul to "64" for example(a numeric value).
sNum is a variable; that means it's gonna change during the execution. So I only can declare the variable sNum at the beginning of my program. I can't declare it as you did below. (sNum[0]='b' and sNum[1]='o').
main()
{
char sNum[1],res;
/*sNum = "bo"*/
sNum[0]='b';
sNum[1]='o';
/* res is the second character of sNum*/
res=sNum[1];
}
Cougar
28 janvier 2004, 20h58
sNum = "what you want when you want where you want"
to get the n-ième char of a string you do : sNum[n-1]
for exemple, if sNum is "to" ,you declare a char (here res) and you write res=sNum[1] to get the second character of the string...
Or use a int instead of string and do (name_of_int)%10 to get the unit of the integer.
Matt
28 janvier 2004, 21h01
Bon, batteur, au lieu de t'emmerder à écrire un espèce d'anglais digne d'un gamin de 6ème qui débute, écrit français ... Parce que j'ai vraiment du mal à croire que t'es pas francophone (ton anglais est plein de gallicisme, et ton pseudo fait quand même très français ...) ...
Sinon, pour ton problème, puisque sNum contient un nombre et que dans ton string, tu veux récupérer le nombre sans son premier chiffre, il suffit d'utiliser l'instruction % (modulo) ...
Par exemple, si tu as sNum == 64, en faisant sNum = sNum % 10, tu auras alors sNum == 4, soit le dernier chiffre de sNum.
Samva
28 janvier 2004, 21h29
C'est pas juste vous avez décidé de jouer sans moi, sinon batteur tu peux utiliser les fonctions atoi() de conversion de char* vers int, mais bon ... vu comme le topic est parti ca va etre fun :)
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.