7 Replies - 257 Views - Last Post: 14 April 2011 - 09:07 AM Rate Topic: -----

#1 UCLA_Tennis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-February 11

Comparing strings at specific characters

Posted 13 April 2011 - 05:16 PM

Is there a way to compare the first two characters of one string to the last two characters of another string?

Example:

drawer
commercial
error
alien

drawer and error would be true, and commercial and alien would be true.
Is This A Good Question/Topic? 0
  • +

Replies To: Comparing strings at specific characters

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,737
  • Joined: 08-August 08

Re: Comparing strings at specific characters

Posted 13 April 2011 - 05:22 PM

Yes, there is.
http://www.cplusplus.../string/substr/
Was This Post Helpful? 1
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1616
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Comparing strings at specific characters

Posted 13 April 2011 - 05:22 PM

ya, compare the zero index to the last index with '=='
str[0]==str[strlen(str)-1]

or in C++ with std::string
str[0]==str[str.size()-1]


edit: oops, i gave you 1 char rather than 2.

This post has been edited by ishkabible: 13 April 2011 - 05:34 PM

Was This Post Helpful? 0
  • +
  • -

#4 nmeans73  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 59
  • Joined: 26-December 09

Re: Comparing strings at specific characters

Posted 13 April 2011 - 05:28 PM

A string is essentially a fancy array of chars. As such, you can access elements with the [] notation. This is assuming you are using c++.

For instance:


string myString = "hello world!";

cout<<myString[3];  //will display the first l in hello




There is also a function in the string class which returns the size.


myString.size(); //will return how many characters are in the string, including whitespace




Since the index of an array/string starts at 0,
 myString.size() - 1; 
will give you the index of the last element in the sting. From there it should be simple to look at both ends of the string.

This post has been edited by nmeans73: 13 April 2011 - 05:29 PM

Was This Post Helpful? 0
  • +
  • -

#5 UCLA_Tennis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-February 11

Re: Comparing strings at specific characters

Posted 13 April 2011 - 05:33 PM

View Postishkabible, on 13 April 2011 - 05:22 PM, said:

ya, compare the zero index to the last index with '=='
str[0]==str[strlen(str)-1]

or in C++ with std::string
str[0]==str[str.size()-1]

Would this just compare the first char with the last char?

View Postnmeans73, on 13 April 2011 - 05:28 PM, said:

A string is essentially a fancy array of chars. As such, you can access elements with the [] notation. This is assuming you are using c++.

For instance:


string myString = "hello world!";

cout<<myString[3];  //will display the first l in hello




There is also a function in the string class which returns the size.


myString.size(); //will return how many characters are in the string, including whitespace




Since the index of an array/string starts at 0,
 myString.size() - 1; 
will give you the index of the last element in the sting. From there it should be simple to look at both ends of the string.


Sorry, I forgot to specify. What if I am using c?
Was This Post Helpful? 0
  • +
  • -

#6 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1616
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Comparing strings at specific characters

Posted 13 April 2011 - 05:35 PM

ya sorry it would just compare 1, my bad.
Was This Post Helpful? 0
  • +
  • -

#7 nmeans73  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 59
  • Joined: 26-December 09

Re: Comparing strings at specific characters

Posted 14 April 2011 - 08:39 AM

Quote

Sorry, I forgot to specify. What if I am using c?


I'm not extremely familiar with the differences between c and c++ syntax so I can't say how the code would be different. The logic however remains the same. Find the number of elements in the one string, subtract 1 to get to the last element, and 2 to get to the second to last element. And you just use the array index to access the first and second elements in the other string.

This post has been edited by nmeans73: 14 April 2011 - 08:41 AM

Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,358
  • Joined: 16-October 07

Re: Comparing strings at specific characters

Posted 14 April 2011 - 09:07 AM

Yep, C is a different beast. No fancy objects. Much simpler. ;)

A string in C is just an array of characters.
char *s1 = "hello"; // a string
int s1Head = 0; // first char position
int s1Tail = strlen(s1)-1; // last char position

//so
printf("first two = %c%c\n", s1[s1Head],s1[s1Head+1]);
printf("last two = %c%c\n", s1[s1Tail-1],s1[s1Tail]);



To compare two, well, imagine an s2 and go from there.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1