![]() |
| | #1 |
| Registered User Join Date: Mar 2010
Posts: 12
| Last edited by BamaMarine06; 03-01-2010 at 04:20 AM. |
| BamaMarine06 is offline | |
| | #2 |
| Registered User Join Date: Feb 2010
Posts: 36
| Post your code up to what you tried so far. |
| vivekraj is offline | |
| | #3 |
| Registered User Join Date: Mar 2010
Posts: 12
| 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;
}
|
| BamaMarine06 is offline | |
| | #4 |
| Registered User Join Date: Nov 2008 Location: INDIA
Posts: 60
| Actually what is your requirement . Whether you want to store each word in a row or reverse a string . |
| karthigayan is offline | |
| | #5 |
| Registered User Join Date: Mar 2010
Posts: 12
| 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, |
| BamaMarine06 is offline | |
| | #6 |
| Registered User Join Date: Nov 2008 Location: INDIA
Posts: 60
| 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 |
| karthigayan is offline | |
| | #7 |
| Registered User Join Date: Mar 2010
Posts: 12
| 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) |
| BamaMarine06 is offline | |
| | #8 |
| Registered User Join Date: Nov 2008 Location: INDIA
Posts: 60
| I think this is your home work. |
| karthigayan is offline | |
| | #9 |
| Registered User Join Date: Mar 2010
Posts: 12
| no, actually I am working problems from a worksheet to prepare me for a quiz |
| BamaMarine06 is offline | |
| | #10 |
| Registered User Join Date: Sep 2006
Posts: 3,720
| 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. |
| Adak is offline | |
| | #11 |
| Registered User Join Date: Mar 2010
Posts: 12
| thank you for explaining that adak |
| BamaMarine06 is offline | |
| | #12 |
| Registered User Join Date: Sep 2006
Posts: 3,720
| 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;
}
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. |
| Adak is offline | |
| | #13 | |
| Registered User Join Date: Jan 2010
Posts: 104
| Quote:
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? | |
| Soulzityr is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| What game programmer should I be? need some advice. | m3rk | General Discussions | 10 | 04-20-2009 11:12 PM |
| I lost my laptop, DVD and money | Sang-drax | A Brief History of Cprogramming.com | 21 | 10-01-2004 07:13 PM |
| Experienced C programmer lost in C++ | hern | C++ Programming | 13 | 04-09-2004 03:03 PM |
| API, LOST... help | Unregistered | Windows Programming | 5 | 03-13-2002 03:19 PM |
| I need to interview professional programmer.....please | incognito | C++ Programming | 1 | 01-05-2002 02:46 PM |