-
need help in some code
i have a code assignment which require a few code to be writen (i m new to c programming)
anyway heres the question, need to perfect it
first one,
Code:
write a c program to count the number of words in its input, where a"ward" is defined as any continuous sequence of non-whitespace characters
example:
input:"+++ --- hi bye 879+3"
output:" the string has 5 words.
("+++","---","bye", and "879+3")
heres my code
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char str[40];
int c;
int i,num=0;
int space;
gets(str);
c=strlen(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==' '&&(str[i+1]!=' '))
{
num++;
}
}
if(str[0]==' ')
{
printf("The string has %d words ",num);
system("pause");
}
else
{
printf("The string has %d words",num);
printf("\n\n");
system("pause");
}
}
2 more code posted below
the 3rd program i have no idea how to write at all
please help me asap
i'm using dev c++ to write the code
thankyou v. much!
-
second one
Code:
write a C program that reads in a string from user and outputs the middle item of the string. stop the program if -1 is read.
Example:
input: "i am busy today"
output: "s"
heres my code
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
int main()
{
char str[40];
int c,mid;
int i;
gets(str);
c=strlen(str);
for(i=0;i<c;i++)
{
if(c%2==0)
mid=str[c/2];
else
mid=str[(c-1)/2];
}
printf("%c",mid);
printf("\n\n");
system("pause");
}
third ones
Code:
write a code which work like this
input : 12345
output: 12543
the output is the middle character of the string & last character of the string switched
input shouldnt be limited.
-
First Program:
Your program is returning an incorrect number of words, so there is something wrong with your logic. This program can be accomplished much more easily with a variable that is used to indicate whether you are in a word or not (let's call it "inWord"). Make "inWord" equal to one if you are in a word (i.e. the current character is not a space) and make "inWord" equal to zero if you are not in a word (i.e. the current character is a space). See if developing the logic based around this idea helps you with the program.
Also, I don't understand why you're using the last "if/else" statement to print the output. The code in each branch is pretty much the same, so there's no use in an "if/else." Just print the output.
Some other notes:
- You should explicitly "return 0;" at the end of "main()"
- You shouldn't use "system("pause")" to keep the console open - "getchar()" would be better
- "gets()" is unsafe (see here) - consider "fgets()" as a safer alternative
--------
Second Program:
Your loop seems to be virtually useless here. Note that you are not using the value of "i" in the loop, so nothing is going to be different for any iteration. What if you tried this instead?
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
int main()
{
char str[40];
int c,mid;
int i;
gets(str);
c=strlen(str);
// for(i=0;i<c;i++)
// {
// if(c%2==0)
mid=str[c/2];
// else
// mid=str[(c-1)/2];
// }
printf("%c",mid);
printf("\n\n");
system("pause");
}
Also, the same suggestions go for this program as the last one (see "Some other notes" above).
--------
For the third program, this doesn't seem too hard if you got the first two working yourself. You simply have to identify the middle character and the last character, and swap them. You already worked out how to identify the middle character. Finding the last character should be just as easy. And swapping them is just a matter of reassigning those indices of the character array (hint: a temporary variable is need to perform this simple swap).
Try out the third one and post your attempt if you can't solve it.
It will definitely help to sketch it out on paper by hand, to get an idea of the steps needed to solve that problem.
-
thanks so much!, i hav successfully solve it