473,394 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

strings and pointer

I have been asked to make a program which can change lowercase letter to upper case without using any given functions from C libraries ( for example toupper from string.h ). It should work like this
Input string to capitalised: input
Enter line of txt : input is wrong
INPUT is wrong.
What I have done is above, do help please!!
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #define maxlen 80
  3. main ()
  4. {
  5.     char line1 [maxlen +1];
  6.     char   string[80];
  7.     char i,c;
  8.     char *str;                    
  9.     char *line=0;
  10.     void readline (char *,int);
  11.     void to_cap_str(char *, char *);
  12.     printf ("Input String to Capitalise:");
  13.     scanf ("%s",string);
  14.     printf ("Enter a line of txt:\n");
  15.     scanf ("%s",line1);
  16.     str = string;
  17.     line = line1;
  18.     to_cap_str(line,str);
  19.     readline (line, maxlen);       
  20. }
  21. void readline (char *line,int length)
  22. {
  23.     int i=0,c;
  24.     while (i++ <length &&(c = getchar ()) != '\n' && c != EOF)
  25.     *line++ = c;
  26.     *line ='\0';
  27. }
  28. void to_cap_str(char *line, char *str)
  29. {
  30.     int i=-1,c=0;
  31.     *str++ = c;
  32.     for (c=0; c<i; c++)
  33.     {
  34.     if (c==i)
  35.     {
  36.         i=i-32;
  37.         i++;
  38.     }
  39.  
  40.     else break;
  41.     }    
  42.     *line++ = i;
  43. }
  44.  
Feb 13 '07 #1
2 1225
I for got to mention that program will not terminated until CTRL+D is issued
Feb 13 '07 #2
AdrianH
1,251 Expert 1GB
I for got to mention that program will not terminated until CTRL+D is issued
Well you have done a good job of it so far. Your readline() is a bit convoluted but it should work (personally I'd have built my format string and used scanf(), but to each their own, yours might be faster, though faster doesn't usually mean much in user input unless you are writing a game).

Your main problem appears to be in to_cap_str(). I'm not sure what you are trying to attempt, but the loop will never execute as c<i is always false (c=0, i=-1).

What you should try is to make a to_cap_str() that capitalises the first n chars in a c-string, and make a find_str() funciton to find the beginning of a string that you want to captilise. Then loop over the string with these functions.

In this way you have broken down the problem into more manageable parts, with each being far more attainable and testable.

NOTE: A C-String is interesting as you can pass a sub string from anywhere in it to the terminating null by passing the address of an indexed element. I.e.
Expand|Select|Wrap|Line Numbers
  1. char str[]="Hello there.";
  2. char * substr = &str[6];
  3. printf("%s", substr);  // output "there."
  4.  
Hope this helps.


Adrian
Feb 13 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Ian Todd | last post by:
Hi, I am trying to read in a list of data from a file. Each line has a string in its first column. This is what i want to read. I could start by saying char to read in 1000 lines to the array( i...
1
by: Jeff | last post by:
I am struggling with the following How do I marshal/access a pointer to an array of strings within a structure Than Jef ----------------------------------------------------------------
89
by: scroopy | last post by:
Hi, I've always used std::string but I'm having to use a 3rd party library that returns const char*s. Given: char* pString1 = "Blah "; const char* pString2 = "Blah Blah"; How do I append...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.