Saturday, May 22, 2010

C++ Question, Strings?

How can i get the last letter of a string in C++?


ex. input = "hi", output = "i"

C++ Question, Strings?
http://cppreference.com/cppstring/length...





You can use an indexer on strings, so the last letter is at length -1.
Reply:char lastchar(string s)


{


if (( s == null ) || ( s.length() == 0 ))


return '\0';





return s[ s.length() - 1 ];


}
Reply:Hello dear,


The first answer is written in C# !


this is C++ code for your question:





char *c ;


gets(c);


int i=strlen(c);


cout%26lt;%26lt;c[i-1]; //Because index of array members in C start from 0





Good Luck , Hossein
Reply:From what I remember you check next character and if it a null or space then output current.
Reply:try this code


for(i=0;i%26lt;str.lenght();i++)


{


if(i==str.lenght())


cout%26lt;%26lt;str[i];


}

flowers on line

No comments:

Post a Comment