Communauté Informatique NDFR.net

Communauté Informatique NDFR.net (http://www.ndfr.net/forums/index.php)
-   Programmation (C/C++, Delphi, VB, etc.) (http://www.ndfr.net/forums/forumdisplay.php?f=64)
-   -   delete the second character of a string? (http://www.ndfr.net/forums/showthread.php?t=3443)

batteur91 28-01-2004 16:42

delete the second character of a string?
 
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-01-2004 17:22

Re : delete the second character of a string?
 
1) You are on a FRENCH forum !!!

2) When you write in english, please try to use correct english grammar and syntax ...

Spycam 28-01-2004 17:49

Re : delete the second character of a string?
 
And what is the language ? C++ ?

batteur91 28-01-2004 18:02

Re : delete the second character of a string?
 
I use the C language.

Cougar 28-01-2004 18:42

Re : delete the second character of a string?
 
Code:


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-01-2004 20:53

Re : delete the second character of a string?
 
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-01-2004 20:58

Re : delete the second character of a string?
 
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-01-2004 21:01

Re : delete the second character of a string?
 
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-01-2004 21:29

Re : delete the second character of a string?
 
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 :)


All times are GMT +2. The time now is 09:56.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.