Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey, I 'am a beginner in C language.
problem 1:
Have i used pointer p and q properly.
problem 2:
After taking the input health care the program just crashes.
health care

after this the program crashes.

What I have tried:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    int i, k = 0, j = 0;
    char line[50];
    char *q, *p;
    
    gets(line);     // health care
    fflush(stdin);  //removing the extra character from the buffer memory (if any)
    
    int n = strlen(line);   //length of the line
    
    for (i = 0; n; i++) 
    {
        if (line[i] == ' ' || line[i] == '\0')      //when a space come taking the first charater of the previous word(h)
        {
            k = 0;  
            if (q[k] >= 'a' && q[k] <= 'z')     // if character is in lowercase (making it to uppercase)
            {
                p[j] = (q[k] + 32);
                j++;
            }
            else        // if already in uppercase (just save it in another different string)
            {
                p[j] = q[k];
                j++;
            }
            continue;
        }

        if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z'))   // taking each word in a different string 
        {
            q[k] = line[i];     //health
            k++;
        }
    }
    
    p[j] = '\0';    //adding null to the final string ('W','H','\0') 
    
    printf("The abbreviate is %s.", p);     //print the final string (WH)
    return 0;
}
Posted
Updated 2-Oct-22 22:20pm

You don't allocate any memory to q or p, nor do you assign them any value.

So when you try to access them, what you get is just a memory access at random and your app crashes.

BUt you should have spotted that for yourself, and you would have if you'd spent a minute or two with the debugger.

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
Comments
Parth Vijay 3-Oct-22 3:34am    
thanks sir, i was not aware of this debug feature in the IDE as I'm an absolute beginner. But after your answer i have learnt it from some utube tutorials and found that there were many mistakes in the code.. Next time, I will first dry run by hands and then if not contented will use this feature..
OriginalGriff 3-Oct-22 4:12am    
Always start with the debugger, and "dry run" with that. It's a load quicker than faffing about and hoping it works!

I'd suggest that you are writing code the wrong way: typing it all up and then running it to see if it
a) Compiles
and
b) Works.
That's a bad idea as it means you can have written a pile of code that is useless because the whole idea is wrong.
Have a look here:
How to Write Code to Solve a Problem, A Beginner's Guide[^]

It may help you get better code, quicker!
CPallini 3-Oct-22 4:35am    
5.
If you just need to 'abbreviate' the input, then you might do it in place, without messing with pointers.
C
#include <stdio.h>
#include <stdlib.h>

int main()
{
  int i = 0;
  char line[50];

  fgets(line, sizeof(line), stdin);
 

  while (line[i])
  {
    char  c = line[i];
    if ( c >='a' && c <='z')
      line[i] = (c-'a'+'A');

    if ( line[i] == ' ')
      break;
    ++i;
  }

  line[i] = '\0';
  printf("%s\n", line);

  return 0;
}
or, alternatively
C
#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main()
{
  char line[50];

  fgets(line, sizeof(line), stdin);
  
  strtok(line, " ");

  for (unsigned n=0; line[n]; ++n)
    line[n] = toupper(line[n]);

  printf("%s\n", line);

  return 0;
}
 
Share this answer
 
v3
Comments
merano99 3-Oct-22 4:26am    
my 5
CPallini 3-Oct-22 4:37am    
Thank you. I removed the fflush calls, as per you advice to the OP :-)
merano99 3-Oct-22 11:35am    
Thank you too. Although it is not quite clear what the desired function of the OP's program is, I would guess that spaces are to be skipped instead of "abbreviating" the input, since he used continue instead of break. Unfortunately, the OP did not explain the meaning of the previously uninitialized variables p and q either. The original program contains parts that will probably never be used in this way. However, the headline then does not fit the program.
If the warnings were switched on with your compiler it would have warned you, that the uninitialized local variables "p" and "q" are used.
C
if (q[k] >= 'a' && q[k] <= 'z')

There are obviously two pointers here that point to "something", or contain a null pointer. If the pointers are to point to a field, then it would be necessary to get the memory for that as well. In addition, the question arises how letters can be contained there at all. So the program makes no sense.

Already at the input the program can crash the first time if someone enters more than 50 characters.
C
char line[50];
gets(line); // health care

With gets() you can't specify the maximum length and besides gets() is no longer supported by some new compilers (removed in C11). You should use gets_s() instead. Alternatively you can use fgets().

en.cppreference.com/w/c/io/gets

One more thing: You should not write functions that are provided by the compiler yourself for no reason.
C
(line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z')

Did you know isalpha() function checks whether a character is an alphabet or not?
C
if (q[k] >= 'a' && q[k] <= 'z')
    p[j] = (q[k] + 32);

Did you know islower() checks if character is lowercase letter and toupper () converts lowercase letter to uppercase?

One more note: According to the C standard, fflush(stdin) causes undefined behavior and should only be used for output streams.
 
Share this answer
 
v2
Comments
CPallini 3-Oct-22 4:35am    
5.
Parth Vijay 3-Oct-22 6:50am    
thank you, for the additional functions now get to know them.. But, i'm using them because i have encountered this situation for the very first time.. So, my approach is learn how their codes are written.. If i don't write them i will never understand how they works (what are logics used to write that fucntion).. In near future i will use these shortcuts to save time;..thnaks again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900