Answered On : Oct 25th, 2005
Take sentence in an array and get array size and then print the array in ascending order.
Answered On : Feb 8th, 2006
A more undo answer can be like this
rever(char a )
{
I think what the question requires is that only the words in the sentence be reversed. For this follow this pseudo code:for each word in sentence: reverse (word)print reverse (sentence)for e.g. given 'an indian on road' after reversing each word we get 'na naidni no daro' on reversing this we get ' road on indian an'
Take the string in an array and continue loop till the half length of array and swap the characters which are at equals from both sides of array.
Answered On : Mar 16th, 2007
Program...
This is a very simple program having following logic. Main string - Life is beautifulCode
#include #include #include void main() { char *s = "Life is beautiful", ch; int len = strlen(s), start, end = -1, t = 0, length = 0, i; clrscr(); *(s + len + 1) = ''; *(s + len) = ' '; while (*(s + length) != NULL) { if (*(s + length) == ' ') { start = end + 1; end = length; //printf("%d %dn",start,end); t = 0; for (i = start; i < start + (end - start) / 2 + 1; i++) { ch = *(s + i); *(s + i) = *(s + end - t); *(s + end - t) = ch; t++; } } length++; } strrev(s); getch(); }
Answered On : Oct 12th, 2007
A small code for this....
Code
#include #include #include void main() { int i, l; char s[] = "this is string reverse"; clrscr(); l = strlen(s); for (i = l; i >= 0; i--) getch(); }
// Note (backslash not displayed on this website.)Code
#include "stdio.h" #include "string.h" #include "stdlib.h" void main() { char *p, ch; int x = 0, y = 0, z = 0; p = malloc(10); scanf("%s", p); z = strlen(p); x = (z) / 2; for (y = 0; y < x; y++) { ch = *(p + y); *(p + y) = *(p - y + z - 1); *(p - y + z - 1) = ch; } return; }
Answered On : Mar 14th, 2008
View all questions by amarendra.moharana View all answers by amarendra.moharana
Hi Srinivas rao i think there is a small change required in ur program
coorect is
l=strlen(s)-1;
Code
#include <stdio.h> #include <assert.h> #define MAX 100 void revStr ( char * ); int main(){ char *s = malloc( sizeof(char) * MAX ); //scanf( "%s", s ); gets( s ); assert( sizeof(s) <= sizeof(char)*MAX ); revStr(s); free( s ); return 0; } void revStr( char *s ){ char *headPtr = s; char *tailPtr = s; while ( *tailPtr != '' ) ++tailPtr; for ( --tailPtr; headPtr < tailPtr; ++headPtr, --tailPtr ){ char temp = *headPtr; *headPtr = *tailPtr; *tailPtr = temp; } }
Answered On : Mar 20th, 2008
View all questions by amarendra.moharana View all answers by amarendra.moharana
Code
int main() { char sentence[50]; cin >> sentence; for (i = 0; i != 'n'; i++) return 0; }
I think by using this code can do the reverse opeartion
when u enter the 'enter key' it will terminate.Code
main() { char c; if ((c = getchar()) != 'n') main(); getch(); }
Answered On : Mar 31st, 2008
View all questions by amarendra.moharana View all answers by amarendra.moharana
// A valid sentence is a group of words ended with a fullstop i.e. with a dot( . ).
int main()
{
char sentence[50];
cin>>sentence; //Every sentence must ended with a dot.
//print the sentence
for(int i=0; i!=' . ' ;i++)
cou<<sentence[i] ;
//reverse the sentence
int length= strlen(sentence);
// suppose the length of the sentence is 10 then fullstop is in the 10th position. For reversing the sentence we have to print from 10th-1 position to the 0th position.
for(int j=length-1; j>=0; j-- )
{
cout<<sentence[j];
}
return 0;
}
Hey srinivasarao,
You are not actually reversing the string !!
You are just printing the string in reverse order.
Answered On : Apr 5th, 2008
View all questions by ashoksurati View all answers by ashoksurati
Let us assume that the entered string is : parents are godsCode
#include #include #include #define size 100 reverse(char *a, char *b) { char temp = *a; *a = *b; *b = temp; } main() { char str[size]; int i, j, len, haf, k = 1; puts("nEnter a string of msx 100 charactersn"); gets(str); len = strlen(str); haf = len / 2; for (i = 0; i < len; i++) for (i = 0, j = haf - 1; i < haf && j > haf; i++, j--) revers(&str[i], &str[j]); for (i = 0; i < len; i++) }
Code
#include<stdio.h> #include<string.h> #include<stdlib.h> void reverseString(char *ch) { int index; int len = strlen(ch); int mid = len / 2; char *temp; for (index = 0; index < mid; index++) { temp = ch[index]; ch[index] = ch[(len - 1) - index]; ch[(len - 1) - index] = temp; } } int main(int argc, char *argv[]) { char *c = (char *) malloc(100 * sizeof(char)); gets(c); reverseString(c); reverseString(c); if (c) { free(c); } return 0; }
I think the question is revere the order of words in the sentence. Because reversing a sentence doesn't make a sense when you can simply copy one string to other in reverse order and do. Here is what I did and it does reverse the order of spaces used in the sentence:
Code
#include <stdio.h> char *reverse_word(char *s) { char *str = s; int len = string_len(s); int i = 0; int count = 0; reverse_substring(str, 0, pos_lastChar(str)); while (i <= len) { if (str[i] == ' ' || i == len) { reverse_substring(str, i - count, i - 1); count = 0; } if (str[i] != ' ') { count++; } i++; } return str; } int pos_lastChar(char *s) { int i; for (i = string_len(s); i > 0; i--) { if (s[i] != ' ' && s[i] != '') { return i; } } } reverse_substring(char *s, int start, int end) { char temp; int i, j; for (i = start, j = end; i < j; i++, j--) { temp = s[i]; s[i] = s[j]; s[j] = temp; } } int string_len(char *str) { int x = 0; while (*str++) { x++; } return (x); } int main(int argc, char **argv) { char str[] = "this is very beautiful"; return 0; }
Code
#include <stdlib.h> int main(void) { const char sentence[] = "When marimba rhythm starts to play"; char *array[50]; int loop; array[0] = (char *) strtok(sentence, " "); if (array[0] == NULL) { exit(0); } for (loop = 1; loop < 50; loop++) { array[loop] = (char *) strtok(NULL, " "); if (array[loop] == NULL) break; } for (loop = 0; loop < 50; loop++) { if (array[loop] == NULL) break; } for (--loop; loop >= 0; loop--) { if (array[loop] != NULL) { } else break; } return 0; }
Code
#include<stdio.h> #include<conio.h> #include<string.h> void main() { char s[50],str[50][50]; int len,i=0,j=0,k=0,l=0,p=0,m; clrscr(); gets(s); len=strlen(s); while(i<len) { j=0; while(1) { if((s[i] == ' ') || (s[i] == '')){ i++; break; } str[k][j++]=s[i];i++; } str[k][j]=''; k++; } /*printf("%c",str[0][0]); */ l=--k; while(l>=0) { p=0; while(str[l][p]!='') { p++; } l--; } getch(); }
Code
main() { char s[100], r[100]; gets(s); int len = strlen(s); char *p = s + len - 1; char *q = r; int j, i = 0, c = 0, flag = 0; while (c <= len) { while (*p == ' ') { p--; for (j = 1; j <= (i - flag); j++) c++; flag = i; } *q = *p; q++; p--; i++; c++; } for (j = 1; j <= (i - flag); j++) }
Here is the code to reverse the sentence that does not use any in built string function. if the input is:Life is good output will be good is Life ;)
Code
#include<stdio.h> void reverse(char *, char *); int main() { /*Declaration and initialization */ char *begin = "Life is Good", *end, *start, *len, *space; start = begin; end = begin; /*Find length of string */ len = begin; while (*len) len++; *len = ' '; *(++len) = ''; len--; /*Reverse individual words */ while (*end) { if (*end == ' ') { space = end; reverse(begin, end - 1); begin = space + 1; end = space; } end++; } *(--end) = ''; /*Reverse the entire string */ char *s1 = ""; s1 = start; "); reverse(start, len - 1); return 0; } void reverse(char *begin, char *end) { char *temp = ""; while (end >= begin + 1) { *temp = *begin; *begin = *end; *end = *temp; end--; begin++; } }
Code
n = strlen(a); for (i = n - 1; i >= 0; i--) { }
Answered On : Dec 13th, 2011
I think you should use reverse string programme. You can store it an array and then run a loop.
Answered On : Mar 21st, 2012
Code
void main() { char *chStrptr = "Hi This is Harish"; While(*ChStrPtr != /0) { LenOfStr++ chStrPtr++; } do { while(*chStrPtr != ) { chStrPtr--; LenOfStr--; } PrintWordsWithNullOrSpace(chStrPtr + 1); }while(LenOfStr != 0); } void PrintWordsWithNullOrSpace(const Char *StrPtr) { while((*strPtr != /0) || (*strPtr != )) { } }
Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.
