Problem with strings.

This is a discussion on Problem with strings. within the C Programming forums, part of the General Programming Boards category; Hi all, my name is attreme and I'm learning to program as an aeronautical engineer. This was supposed to be ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    3

    Problem with strings.

    Hi all, my name is attreme and I'm learning to program as an aeronautical engineer.
    This was supposed to be a simple program that would read four strings and then print only the ones that started witht the letter "b." When I run the program, I don't get any error messages, but the program immediatly exits with 0. I believe that my problem is in the string "si." I wanted it to change the string from s1 to s2 and so forth, but I think it's just not reading. Please help.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int main() {
        char s1[5] = "abcd", s2[5] = "bcde", s3[5] = "bdef", s4[5] = "defg";
        int i;
        char sj[5] = "aaaa";
        char si[5];
        for (i = 0; i <=4; i++){
            strcpy (sj, si);
            if ((strncmp (sj, s2, 1)) == 0){
                printf ("%s", sj);
            }
        }
        return 0;
    }
    Thanks all.

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162

    Post

    Try This
    Code:
    #include<stdio.h>
    
    
    int main() {
        char s[5][5] = {"abcd","bcde","bdef","defg","bact"};
        int i;
        for (i = 0; i <=4; i++)
    	{
         
            if (s[i][0]=='b')
    		{
                printf ("\n%s", s[i]);
            }
        }
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    Using strings within an array instead so that I could call each one by one instead of individual strings...hadn't thought of that.
    Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 09:01 PM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  3. Problem with Strings and Conversions
    By patso in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 01:01 PM
  4. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 01:07 PM
  5. copy strings problem
    By dgcampos in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 09:05 PM

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