Thread: So lost; new c programmer

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    16

    Unhappy So lost; new c programmer

    Working on a problem and cannot figure out this part.....
    Last edited by BamaMarine06; 03-01-2010 at 04:20 AM.

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    Post your code up to what you tried so far.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    16
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    #define STR_LEN 30
    #define N 10
    
    int main(void)
    
    {
        int a[30] [20];
    
    
        char str[STR_LEN + 1];
        printf("Enter a sentence in all lowercase letters ending with a ''.'', ''?'', or ''!'' : \n" );
            for(i = 0; i < N; i++)
                scanf("%s", str);
    
    
        printf("Reversal of sentence:" );
            for(i = N - 1; i >= 0; i--)
                printf("%d", a[i]);
            printf("\n");
    
    
    
    
    
    
    
    
    
    
    
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64

    Question

    Actually what is your requirement . Whether you want to store each word in a row or reverse a string .

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    16
    Right now i'm working on storing each word in a row; the reverse part i haven't got to yet. Their are like 4 different things that are wanted out of this question, but right now the first step is for a person to enter a sentence ending with a . ? or ! and then I have to store what they enter as a row of char array. I have been on this for about 4 dang hours,

  6. #6
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64

    Thumbs up Try this

    ok.I think my code will help you.

    I used a newline as a sentence terminator.You can change it to '.' or '?' or '!'.
    Code:
    #include<stdio.h>
    #include<string.h>
    
    main()
    {
            char grades[30][20];
            int i=0,j=0,k=0;
            char c;
            printf("Enter the sentence : ");
            while((c=getchar()))
            {
                    if(c==' ' || c=='\n' || c=='\t')
                    {
                     grades[i][j]='\0';  // To add a null the make it as a separate string
                            i++;
                            j=0;
                    }
                    else
                    {
                     grades[i][j]=c; assign the character in the array 
                     j++;
                    }
    
                    if(c=='\n')
                            break; terminate if the sentence get over.
            }
    
            for(j=0;j<i;j++)
                    printf("%s\n",grades[j]);
    }


    Thanks

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    16
    ok, thank you so much, can you break this code down to exactly where I am looking to take what they enter and store it into a char array? (sorry, very new c programmer)

  8. #8
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64

    Exclamation

    I think this is your home work.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    16
    no, actually I am working problems from a worksheet to prepare me for a quiz

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I think the first big concept in dealing with strings or char's is this:

    Now is the time for all good men to come to the aid of their country.

    That may look like a sentence, and therefore a string, but NOT in C!!

    In C, a bunch of chars, are just a bunch of chars, *unless* they have an end of string char immediately after them, to mark it as a string.

    This is the end of string char: '\0', and this is a string in C:

    Now is the time for all good men to come to the aid of their country.'\0'

    Whether the char's include a newline: '\n' char or not, doesn't matter. That just changes how the chars will be printed or displayed.

    Usually, you want to deal with chars, as a single string. Not always, but 95% of the time, you do. And it has to have that end of string char, or all your string functions you want to use, like printing for instance:

    printf("%s", mystringArrayName);

    will not work right.

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    16
    thank you for explaining that adak

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Let's look at Karthigayan's program snippet:

    Code:
    #include<stdio.h>  //you'll almost always want this for basic i/o functions, etc.
    #include<string.h> //for working with the string functions.
    
    int main()       //in C this should always be int main() for compatibility reasons
    {
            char grades[30][20];  //our char array: 30 rows, 20 columns across
            int i=0,j=0,k=0;          //a few int's, all assigned zero value
            char c;                    //a single char 
    
    Question1: What can a single char NEVER become in C?
    
            printf("Enter the sentence : ");
            while((c=getchar()))  //get one char while they're in the keyboard buffer
            {
                    //if the char is a space, newline or tab
                    if(c==' ' || c=='\n' || c=='\t')
                    {
                     //add the end of string char to the array
                     grades[i][j]='\0';  // To add a null the make it as a separate string
                            i++;            //increment the row number
                            j=0;            //set the column number to zero.
                    }
                    else                     //it wasn't a space, newline or tab char
                    {
                     grades[i][j]=c;      //assign the character in the array 
                     j++;                    //increment the column number, ready for the next char
                    }
    
                    if(c=='\n')  
                            break;         //terminate if the sentence is finished.
            }
    
            for(j=0;j<i;j++)                      //for each row
                    printf("%s\n",grades[j]);  //print the string we made
    
            /* this is the int being returned to the operating system. It can be used to find out
                if the program finished right or not.
            */
            return 0;
    }
    Nice clear and simple logic, and nice indentation - which is important in C, because the code becomes so much easier to read. Especially after you've done it for awhile, your eye just gets trained to catch the errors with standard indentation of subordinate lines of code.




    Question 2: What punctuation mark is not included in this program, that ends most sentences?

    Question 3: This is used in the program: == What's the difference between = and == and which one do you always want to use inside an if statement?

















    ================================================== ===

    1) A string. It doesn't have enough space to hold a char, and the end of string char.

    2) A period.

    3) One = is assignment: x = 3; Two == is a comparison: if(x == 2). Two =='s are always used in an if statement.

  13. #13
    Registered User
    Join Date
    Jan 2010
    Posts
    103
    Quote Originally Posted by Adak View Post
    I think the first big concept in dealing with strings or char's is this:

    Now is the time for all good men to come to the aid of their country.

    That may look like a sentence, and therefore a string, but NOT in C!!

    In C, a bunch of chars, are just a bunch of chars, *unless* they have an end of string char immediately after them, to mark it as a string.

    This is the end of string char: '\0', and this is a string in C:

    Now is the time for all good men to come to the aid of their country.'\0'

    Whether the char's include a newline: '\n' char or not, doesn't matter. That just changes how the chars will be printed or displayed.

    Usually, you want to deal with chars, as a single string. Not always, but 95% of the time, you do. And it has to have that end of string char, or all your string functions you want to use, like printing for instance:

    printf("%s", mystringArrayName);

    will not work right.
    Hey Adak, I have a question about what you said here. When am I supposed to use '\0'?? Ive been fscanf strings for the past 2-3 programs and I've never heard of this, o_O. so have i been doing it wrong? I have always come up with many problems getting the strings or so but ive always fixed them never seeing the '\0' rule.

    Is that why I have my current problem for string here?

    output of struct? confused....

    the name part, instead of printing jas its printing H*n. is it because I don't have some '\0' somehow?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What game programmer should I be? need some advice.
    By m3rk in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-20-2009, 11:12 PM
  2. I lost my laptop, DVD and money
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-01-2004, 07:13 PM
  3. Experienced C programmer lost in C++
    By hern in forum C++ Programming
    Replies: 13
    Last Post: 04-09-2004, 03:03 PM
  4. API, LOST... help
    By Unregistered in forum Windows Programming
    Replies: 5
    Last Post: 03-13-2002, 03:19 PM
  5. I need to interview professional programmer.....please
    By incognito in forum C++ Programming
    Replies: 1
    Last Post: 01-05-2002, 02:46 PM