How to compare strings in c++

This is a discussion on How to compare strings in c++ within the C++ Programming forums, part of the General Programming Boards category; how do you compare strings using only one header file i.e. #include<cstdio> Im a noob so i might get stuff ...

  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
    22,303
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    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, 06:09 PM
  4. Trying to compare to strings...
    By imortal in forum C++ Programming
    Replies: 9
    Last Post: 05-16-2003, 01:27 PM
  5. how do i compare strings
    By bart in forum C++ Programming
    Replies: 17
    Last Post: 08-30-2001, 10:17 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21