Thread: How to compare strings in c++

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    1

    How to compare strings in c++

    how do you compare strings using only one header file i.e. #include<cstdio>
    Im a noob so i might get stuff wrong sorry.
    Why doesnt this work:
    Code:
    int i= 0;
    int check=5;
    while(a[i]==b[i]){
    i++;
    }
    if(i==check){
    printf("yess");
    }else{
    printf("nooo")

  2. #2
    Registered User
    Join Date
    Aug 2012
    Posts
    1
    i think

    Code:
    int i = 0;
    while (a[i] && b[i] && b[i] == a[i])
        i++;
    if( a[i] == b[i])
        printf("yess");
    else
        printf("nooo")

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sanscorp1999
    how do you compare strings using only one header file i.e. #include<cstdio>
    Considering that <cstdio> is not about std::string objects or null terminated strings, it sounds like you are really asking how to write a function to compare two null terminated strings, without calling any standard library function in its implementation.

    Quote Originally Posted by Sanscorp1999
    Why doesnt this work:
    You should provide the smallest and simplest compilable program that demonstrates the problem and tell us how does it not work. That said, linhlonet has a made what I believe is a good guess in post #2, but you really should leave us guessing like that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And why are you using printf?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare two strings
    By cable in forum C Programming
    Replies: 3
    Last Post: 02-07-2012, 09:12 AM
  2. compare strings
    By jamal in forum C Programming
    Replies: 6
    Last Post: 11-27-2011, 08:09 PM
  3. Compare Two Strings?
    By Paul22000 in forum C++ Programming
    Replies: 9
    Last Post: 05-24-2008, 05:09 PM
  4. Trying to compare to strings...
    By imortal in forum C++ Programming
    Replies: 9
    Last Post: 05-16-2003, 12:27 PM
  5. how do i compare strings
    By bart in forum C++ Programming
    Replies: 17
    Last Post: 08-30-2001, 09:17 PM

Tags for this Thread