Like Tree2Likes
  • 1 Post By stabu
  • 1 Post By spaghetticode

print first word of the string

This is a discussion on print first word of the string within the C Programming forums, part of the General Programming Boards category; 1. Create a variable named index and nitialize it to zero(0) 2. Prompt for and input a string value from ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    3

    Cool print first word of the string

    1. Create a variable named index and nitialize it to zero(0)
    2. Prompt for and input a string value from thekeyboard. Store the string inthe string variable newstring[80].
    3. While (newstring[index] does not equal ‘\0’).
    i. Display the character at newstring[index] followed by a NL
    ii. Increment index
    ======================================…
    and this is what i have done so far and i dont know where i am wrong
    Code:
    #include<stdio.h>
    
    
    int main()
    {
    int index = 0; //initialize index to zero since first elementin an array is numbered zero 
    
    
    char newstring[80]; 
    
    
    printf("Enter a string\n"); 
    scanf("%s", newstring); 
    
    while(newstring[index] != NULL)
    
    {
    
    
    newstring[index] = Null; 
    index++;
    
    
    
    
    }
    printf("You entered the string: %s\n",newstring);
    
    return 0;
    }
    
    

  2. #2
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    2. Prompt for and input a string value from thekeyboard. Store the string inthe string variable newstring[80].
    C doesn't have a keyboard, string values, or string variables. You probably want another programming language.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    82
    I am not sure what the word Null is doing there. I also don't know what it is. I am sure you know that it is not the same as "NULL".

    Also the assignment says find the "first word" that's the set of characters before the first space. Not the entire string.

    So where you are wrong is that you have not read the assignment properly (or, at least, enough times) and that you are writing to the string while the index is increasing. Also you need to revise that a string is an array of characters, and you need to examine if either NULL or Null are proper characters. The assignment mentions '\0' ... I think you should follow its example ... ' \0' is a much better choice instead of NULL or Null.
    Miss Punjaban likes this.

  4. #4
    language hopper spaghetticode's Avatar
    Join Date
    Jan 2011
    Location
    Hesse, Germany
    Posts
    259
    The assignment literally gives you the algorithm. All you need to do is translate it into C. Compare the steps to your code, especially step 3. What does the assignment want you to do? What do you do?

    Check your while loop condition (stabu gave you a hint there). Check your first expression in the loop's body.
    Miss Punjaban likes this.
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8 | Acer Aspire V3-771G
    OS: Debian Stretch (Testing) | Linux 4.1.0-2-amd64 | KDE Plasma 5.3.2
    Compiler: gcc 5.2.1 20150903

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to reverse a string word by word?
    By SuperMiguel in forum C Programming
    Replies: 22
    Last Post: 03-29-2012, 01:40 AM
  2. Replies: 3
    Last Post: 11-13-2011, 09:23 PM
  3. Replies: 28
    Last Post: 10-23-2011, 08: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