473,473 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Hello, i would love to compare a number of strings, but strcmp allows me to only comp

1 New Member
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include<strings.h>
  5.  
  6. int main(){
  7.     char Entry[10];
  8.     char Delete[10] = "Delete";
  9.     char Reverse[10] = "Reverse";
  10.     char Double[10] = "Double";
  11.     char Replace[10] = "Replace";
  12.     char Decrypt[10]="Decrypt";
  13.     char Encrypt[10]="Encrypt";
  14.     char Quit[10] = "Quit";
  15.     char str[10];
  16.  
  17.     scanf("%s,%s,%s,%s",Double,Delete,Reverse,Replace);
  18.  if (strcmp(Entry,Double)==0 && strcmp(Entry,Delete)==0 && strcmp(Entry,Reverse)==0 && strcmp(Entry,Replace)==0)
  19.  {
  20.  
  21.  printf("\nyes");
  22. }
  23.  else printf("\ninvalid inputs!");
  24.  
  25.     return 0;
  26. }
Jul 25 '18 #1
3 1866
weaknessforcats
9,208 Recognized Expert Moderator Expert
I am not sure what you need to do.

However, strcmp returns 0 when the strings are equal. A 0 is false. Therefore, you need to use the not operator (!) when using strcmp inside an "if" statement:

Expand|Select|Wrap|Line Numbers
  1. if (!strcmp(strA, strB))
  2. {
  3.    printf("strings are equal\n");
  4. }
Jul 25 '18 #2
dev7060
638 Recognized Expert Contributor
You can use nested ifs to compare several strings using strcmp. Like:

Expand|Select|Wrap|Line Numbers
  1. if(!strcmp(A, B)){
  2.    if(!strcmp(A, C)){
  3.        //Print strings A, B and C are equal
  4.    }
  5. }
  6.  
Aug 18 '18 #3
donbock
2,426 Recognized Expert Top Contributor
scanf() on line 17 overwrites strings Double, Delete, Reverse, and Replace; so the initializers on lines 8-11 are useless.

If you don’t intend for the strings on lines 8-14 to be changed then you should declare them as const char arrays. If these strings are const then there is no need to specify the array size — use empty brackets([ ])

The Entry array is uninitialized.

Do you perhaps want || (or) instead of && (and) on line 18? Entry can only be equal to Double, Delete, Reverse, and Replace if somehow those 4 strings are equal to each other.

Lines 21 and 23. Might be best to add newline to the end of each printed string.

Line 23. I suggest you braces around the printf() like you have on lines 19-22 so you don’t get confused about what happens here.

Lines 19-23. Indentation is inconsistent with rest of function. This might cause confusion.
Aug 21 '18 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Lian | last post by:
Hi all, I am confused by the rule of comparing two unicode string using function "strcmp". For exmaple, using "strcmp" to compare two Chinese uincode string, depending on what rule can "PHP"...
1
by: Rolf Kemper | last post by:
Dear All, does have anybody have an idea how to add to number strings in xslt ? My strings are in variables eg. '1A2' , '00F' which should sum up to '1B1'. I may prefix the variables with...
8
by: vonclausowitz | last post by:
Hi Everyone, This is a very complicated, if not impossible problem. I have two strings I want to compare. The problem is that somewhere in both strings there is a text which is identical, but...
3
by: Sasha | last post by:
Hi everyone, Here is my problem: I have the following classes: - DataNode - this class is designed to hold some data and will be contained in a tree like data structure DataTree. When...
4
by: Sasidhar Parvatham | last post by:
Hi All, How do I compare two strings and get all the positions where there is a difference between them? Thanks, Sasidhar
3
by: Twinkle | last post by:
HI there i want to compare between two strings char by char.every strings having a word document.first string name is strFileName and second string name is strFilename1. i want to compare...
0
by: ev951 | last post by:
I am not that familiar with XML or XSL and I am trying to sort application version number strings in an XML file that my team uses for application installations on our Linux servers. I have tried...
2
by: Peter Proost | last post by:
Hi group, I want to compare path strings in order to sort them, assuming I have got: "a.txt" "dir1\c.txt" "e.txt" "dir1\d.txt" When I compare them using "e.text" would be greater than...
1
by: Totally Stumped | last post by:
I'm getting a bad operand types for operator <. first String. second string. I get want the error means, so I'm assuming that I can't use this operator to sort my array of objects. Is there some...
1
by: deego | last post by:
I need to compare two strings in a word document i work it out for alphabetic and numeric values but it is not able to compare a string which contains some special characters like (Máceres-Cartínez)...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.