Thread: C question...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    101

    C question...

    char s1[50];
    char* s2[50]={"Peter","John","Mary","Tom",.....};
    char* s3[50]={"...",".-.","-.-","---",......};
    int i=0;
    printf("Enter a string: ");
    gets(s1);
    for (i;s1[i] != '\0';i++){
    for (j;j <50; j++){
    if (s1[i] == *s2[j])
    puts(s3[j]);
    }
    }
    if i want to check s1 if same as s2, than i will print s3. how to do it...
    thank you

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    To compare strings, you don't use ==

    Use strcmp

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Code:
    if (strcmp (string1, string2) == 0)
        /* Then both strings are equal */

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Dear All,
    I want to input the ..-.., if I input like s3[], than print s2[], but I can't do it, please help me to fix....
    Thank you!!

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define ArraySize 39
    main(){

    char* s1;
    char s2[ArraySize]="?,.ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    char *s3[ArraySize]={"..--..","--..--",".-.-.-",".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","-----",".----","..---","...--","....-",".....","-....","--...","---..","----."};
    int i,j;
    printf("Enter a String: ");

    scanf("%s ", *s1);


    for (i=0; s1[i] != '\0'; s1++)
    {

    for (j=0;j < ArraySize; j++){
    if (*s1[i]== *s3[j])
    putchar(s2[j]);
    }
    printf(" ");
    }
    getchar();
    return 0;

    }

  5. #5
    Unregistered
    Guest

    Smile

    Hey try replacing ya code with this...


    int i,j;
    printf("Enter a String: ");
    scanf("%s ", s1);

    for (i=0; s1[i] != '\0'; i++) {
    for (j=0;j < ArraySize; j++){
    if (s1[i]== s3[j])
    printf("%s",s2[j]);
    }
    }
    getchar();
    return 0;
    }


    Note: Consider changing the decleration "char *s1" to "char s1[MAXNO]" where you define MAXNO a maximum number you prefer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM