Hello.
How can I take a substring of lower case caracters of a String.
Examples:
If i have : COmpuTER
Output: mpu
If i have : ComputER
Output : omput
Sorry, about my english.
Thanks.
Hello.
How can I take a substring of lower case caracters of a String.
Examples:
If i have : COmpuTER
Output: mpu
If i have : ComputER
Output : omput
Sorry, about my english.
Thanks.
Loop through all the characters of the source string. In the loop, use islower to test whether the character is lowercase; if so, copy the character from the source string to the destination string. Remember to null terminate the destination string after the loop exits.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
What would you do in the case of 2 substrings like "ComPUteR"? Would you merge the two or ignore one of them?
If you understand what you're doing, you're not learning anything.
The two.
But i had already did it.
Thanks.
But i have a problem!
The output is printting other strange characters in the place of the non lower case characters.
Example: COMputeR
Output : $"!#pute!
Help me!
Thanks.
Like this:
COMputER
Output :?? put???n @
It is far easier for others to help you debug your code if you actually post it (preferably within code tags: [code][/code]).
From the output, my guess is that you are using the source index in the destination string and an uninitialized destination string. Use a separate index for the destination string, increment it after writing a lowercase character.
Last edited by Dave_Sinkula; 10-15-2004 at 09:07 AM.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Code:#include <stdio.h> #include <ctype.h> main() { char input[50]; char new[50]; int i = 0; int j = 0; fgets(input,50,stdin); for(i = 0;i<strlen(input);i++) { if(islower(input[i])) { new[i] = input[i]; } } printf("%s\n", new); }
Code:#include <stdio.h> #include <ctype.h> #include <string.h> int main(void) { char input[50]; char new[50]; int i = 0; int j = 0; fgets(input,50,stdin); for ( i = 0;i<strlen(input);i++ ) { if ( islower(input[i]) ) { new[j++] = input[i]; } } new[j] = '\0'; printf("%s\n", new); return 0; }
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Having the separate j counter is the right approach, but you don't actually use it anywhere.
If you understand what you're doing, you're not learning anything.
you have to use two counters because if you only used one take the examplethe counter goes to 2(0 then 1 then 2) so then it puts the lower case letter in new[2] right? What is before the 2 the 0th and the 1st elements of the array(string might be easier to understand) The answer is random garbage. Then you have to end the string so that C knows it is finished. that is why he put a '\0' at the last element of his array(string). Understand?Code:JOhn
I have changed the if statement to give the lower case sequence that begin with a lower case vowels:
char vowels[] = "aeiou";
/*I do a cycle to verify vowels*/
if(islower(input[i]) && (input[i] == vowels[j])) {
Actually i want to do a program that reads a string.
Then i want after a sequence of lower case sequence beginning with a lower case vowel, insert a '&';
Example: MargtRTY45efghtAQrtyP
Output : Margt&RTY45efght&AQrtyP
Separating the lower case sequence beginning with a lower case vowel.
Then I make some kind of "APPEND", putting the '&' in the end of the substrings.
Then CONCAT and.......
And return again that string formatted.
I dont know if is possible to do this by this process.
When this came up last time, I thought your question had been answered. During that thread, I had cobbled up this (except for the change from # to & and your latest example text).Code:#include <stdio.h> #include <ctype.h> char *foo(char *dst, const char *src) { static const char vowel[] = "aeiou"; int i, j, k = 0, seq = 0; for ( i = 0; src[i]; ++i ) { if ( islower(src[i]) ) { for ( j = 0; vowel[j]; ++j ) { if ( src[i] == vowel[j] ) { seq = 1; break; } } } else { if ( seq ) { dst[k++] = '&'; } seq = 0; } dst[k++] = src[i]; } if ( seq ) { dst[k++] = '&'; } dst[k] = '\0'; return dst; } int main() { char text[] = "MargtRTY45efghtAQrtyP"; char result [ 2 * sizeof text - 1 ]; printf("text = \"%s\"\n", text); printf("result = \"%s\"\n", foo(result, text)); return 0; } /* my output text = "MargtRTY45efghtAQrtyP" result = "Margt&RTY45efght&AQrtyP" */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
But in the last time the special character always appeared at the front of the vowel.
Now i will try to do my code lokking sometimes to your code!
Thanks very much.